Search in sources :

Example 1 with CommandHandler

use of meghanada.server.CommandHandler in project meghanada-server by mopemope.

the class EmacsServer method dispatch.

private boolean dispatch(final List<String> argList, final CommandHandler handler) {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    id++;
    final boolean result = match(argList).when(headTail(eq("pc"), any())).get(args -> {
        // pc : Project Change
        // usage: pc <filepath>
        handler.changeProject(id, args.get(0));
        return true;
    }).when(headTail(eq("p"), any())).get(args -> {
        // p : Parse
        // usage: p <filepath>
        handler.parse(id, args.get(0));
        return true;
    }).when(headTail(eq("ap"), any())).get(args -> {
        // ap : Autocomplete Prefix
        // usage: ap <filepath> <line> <column> <prefix> <fmt>
        handler.autocomplete(id, args.get(0), args.get(1), args.get(2), args.get(3));
        return true;
    }).when(headTail(eq("c"), any())).get(args -> {
        // c : Compile
        // usage: c <filepath>
        handler.compile(id, args.get(0));
        return true;
    }).when(headTail(eq("cp"), any())).get(args -> {
        // cp : Compile Project
        // usage: cp <filepath>
        handler.compileProject(id, args.get(0));
        return true;
    }).when(headTail(eq("fc"), any())).get(args -> {
        // fc : Format code
        // usage: fc <filepath>
        handler.formatCode(id, args.get(0));
        return true;
    }).when(headTail(eq("di"), any())).get(args -> {
        // di : Diagnostic
        // usage: di <filepath>
        handler.diagnostics(id, args.get(0));
        return true;
    }).when(headTail(eq("dl"), any())).get(args -> {
        // dl : Diagnostic live
        // usage: dl <filepath> <contents-file>
        handler.diagnostics(id, args.get(0), args.get(1));
        return true;
    }).when(headTail(eq("rj"), any())).get(args -> {
        // usage: rj <path>, <testName>
        if (args.size() == 1) {
            handler.runJUnit(id, args.get(0), "", false);
        } else {
            handler.runJUnit(id, args.get(0), args.get(1), false);
        }
        return true;
    }).when(headTail(eq("dj"), any())).get(args -> {
        // usage: dj <path>, <testName>
        if (args.size() == 1) {
            handler.runJUnit(id, args.get(0), "", true);
        } else {
            handler.runJUnit(id, args.get(0), args.get(1), true);
        }
        return true;
    }).when(headTail(eq("rt"), any())).get(args -> {
        // rj : Run Task
        // usage: rt <args>
        handler.runTask(id, args);
        return true;
    }).when(headTail(eq("ai"), any())).get(args -> {
        // ai : Add Import
        // usage: ai <filepath> <import>
        handler.addImport(id, args.get(0), args.get(1));
        return true;
    }).when(headTail(eq("oi"), any())).get(args -> {
        // oi : Optimize Import
        // usage: oi <filepath>
        handler.optimizeImport(id, args.get(0));
        return true;
    }).when(headTail(eq("ia"), any())).get(args -> {
        // ia : Import All
        // usage: ia <filepath>
        handler.importAll(id, args.get(0));
        return true;
    }).when(headTail(eq("st"), any())).get(args -> {
        // st : Switch test to src or src to test
        // usage: st <filepath>
        handler.switchTest(id, args.get(0));
        return true;
    }).when(headTail(eq("jd"), any())).get(args -> {
        // jd : Jump Declaration
        // usage: jd <filepath> <line> <column> <symbol>
        handler.jumpDeclaration(id, args.get(0), args.get(1), args.get(2), args.get(3));
        return true;
    }).when(headTail(eq("sd"), any())).get(args -> {
        // sd : Show declaration (short)
        // usage: sd <filepath> <line> <column> <symbol>
        handler.showDeclaration(id, args.get(0), args.get(1), args.get(2), args.get(3));
        return true;
    }).when(headTail(eq("re"), any())).get(args -> {
        // re : References
        // usage: re <filepath> <line> <column> <symbol>
        handler.reference(id, args.get(0), args.get(1), args.get(2), args.get(3));
        return true;
    }).when(headTail(eq("ti"), any())).get(args -> {
        // ti : TypeInfo (short)
        // usage: ti <filepath> <line> <column> <symbol>
        handler.typeInfo(id, args.get(0), args.get(1), args.get(2), args.get(3));
        return true;
    }).when(headTail(eq("bj"), any())).get(args -> {
        // bj : Back Jump
        // usage: bj
        handler.backJump(id);
        return true;
    }).when(headTail(eq("cc"), any())).get(args -> {
        // cc : Clear cache
        // usage: cc
        handler.clearCache(id);
        return true;
    }).when(headTail(eq("lv"), any())).get(args -> {
        // lv : Local variable
        // usage: lv file line
        handler.localVariable(id, args.get(0), args.get(1));
        return true;
    }).when(headTail(eq("ping"), any())).get(args -> {
        // st : Switch test to src or src to test
        // usage: st <filepath>
        handler.ping(id);
        return true;
    }).when(headTail(eq("kp"), any())).get(args -> {
        // kp : Kill running process
        // usage: kp
        handler.killRunningProcess(id);
        return true;
    }).when(headTail(eq("em"), any())).get(args -> {
        // em : Exec main
        // usage: em <path>
        handler.execMain(id, args.get(0), false);
        return true;
    }).when(headTail(eq("dm"), any())).get(args -> {
        // dm : Debug main
        // usage: dm <path>
        handler.execMain(id, args.get(0), true);
        return true;
    }).when(headNil(eq("q"))).get(() -> {
        // usage: q
        return false;
    }).getMatch();
    log.info("receive command {}. elapsed:{}", argList, stopwatch.stop());
    return result;
}
Also used : Socket(java.net.Socket) Stopwatch(com.google.common.base.Stopwatch) ListConsCases.headNil(com.leacox.motif.cases.ListConsCases.headNil) MatchesExact.eq(com.leacox.motif.MatchesExact.eq) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) Future(java.util.concurrent.Future) Server(meghanada.server.Server) OutputFormatter(meghanada.server.OutputFormatter) OutputStreamWriter(java.io.OutputStreamWriter) Objects.isNull(java.util.Objects.isNull) ExecutorService(java.util.concurrent.ExecutorService) SexpOutputFormatter(meghanada.server.formatter.SexpOutputFormatter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) BufferedWriter(java.io.BufferedWriter) ListConsCases.headTail(com.leacox.motif.cases.ListConsCases.headTail) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Session(meghanada.session.Session) Logger(org.apache.logging.log4j.Logger) Motif.match(com.leacox.motif.Motif.match) MatchesAny.any(com.leacox.motif.MatchesAny.any) BufferedReader(java.io.BufferedReader) Objects.nonNull(java.util.Objects.nonNull) CommandHandler(meghanada.server.CommandHandler) LogManager(org.apache.logging.log4j.LogManager) Stopwatch(com.google.common.base.Stopwatch)

Example 2 with CommandHandler

use of meghanada.server.CommandHandler in project meghanada-server by mopemope.

the class EmacsServer method acceptConnection.

private Future<?> acceptConnection(final Socket conn) {
    return this.executorService.submit(() -> {
        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8));
            final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), UTF_8))) {
            final CommandHandler handler = new CommandHandler(session, writer, getOutputFormatter());
            boolean start = true;
            final SExprParser parser = new SExprParser();
            while (start) {
                final String line = reader.readLine();
                if (isNull(line) || line.isEmpty()) {
                    log.info("close from client ...");
                    break;
                }
                final SExprParser.SExpr expr = parser.parse(line);
                final List<SExprParser.SExpr> lst = expr.value();
                final List<String> args = lst.stream().map(sExpr -> sExpr.value().toString()).collect(Collectors.toList());
                log.debug("receive command line:{} expr:{} args:{}", line, expr, args);
                start = dispatch(args, handler);
                if (!start) {
                    log.info("stop client ... args:{}", args);
                }
                if (this.outputEOT) {
                    writer.write(EmacsServer.EOT);
                    writer.newLine();
                }
                writer.flush();
            }
            log.info("close client ...");
        } catch (Throwable e) {
            log.catching(e);
        } finally {
            try {
                conn.close();
            } catch (IOException e) {
                log.catching(e);
            }
            log.info("client disconnect");
        }
    });
}
Also used : Socket(java.net.Socket) Stopwatch(com.google.common.base.Stopwatch) ListConsCases.headNil(com.leacox.motif.cases.ListConsCases.headNil) MatchesExact.eq(com.leacox.motif.MatchesExact.eq) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) Future(java.util.concurrent.Future) Server(meghanada.server.Server) OutputFormatter(meghanada.server.OutputFormatter) OutputStreamWriter(java.io.OutputStreamWriter) Objects.isNull(java.util.Objects.isNull) ExecutorService(java.util.concurrent.ExecutorService) SexpOutputFormatter(meghanada.server.formatter.SexpOutputFormatter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) BufferedWriter(java.io.BufferedWriter) ListConsCases.headTail(com.leacox.motif.cases.ListConsCases.headTail) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Session(meghanada.session.Session) Logger(org.apache.logging.log4j.Logger) Motif.match(com.leacox.motif.Motif.match) MatchesAny.any(com.leacox.motif.MatchesAny.any) BufferedReader(java.io.BufferedReader) Objects.nonNull(java.util.Objects.nonNull) CommandHandler(meghanada.server.CommandHandler) LogManager(org.apache.logging.log4j.LogManager) InputStreamReader(java.io.InputStreamReader) CommandHandler(meghanada.server.CommandHandler) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)2 MatchesAny.any (com.leacox.motif.MatchesAny.any)2 MatchesExact.eq (com.leacox.motif.MatchesExact.eq)2 Motif.match (com.leacox.motif.Motif.match)2 ListConsCases.headNil (com.leacox.motif.cases.ListConsCases.headNil)2 ListConsCases.headTail (com.leacox.motif.cases.ListConsCases.headTail)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 OutputStreamWriter (java.io.OutputStreamWriter)2 InetAddress (java.net.InetAddress)2 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects.isNull (java.util.Objects.isNull)2 Objects.nonNull (java.util.Objects.nonNull)2 ExecutionException (java.util.concurrent.ExecutionException)2