我现在正在做的是识别光标点的当前范围,然后根据范围使用哈希图提供完成。
HashMap<String,String[]> program =new HashMap<String, String[]>(){{
put("function", new String[]{"Function", "function functionName(){\n\t\n}}"});
put("if", new String[]{"If", "if (true) {\n\t\n}"});
put("try", new String[]{"Try", "try {\n\t\n}}catch(err) {\nconsole.log(err)}"});
put("class", new String[]{"Class", "Class className {\n\t\n}}"});
put("for", new String[]{"For", "for (var i=0; i<10; i++) {\n\t\n}}"});
put("while", new String[]{"While", "while (true) {\n\t\n}}"});
put("do", new String[]{"Do", "do {\n\t\n}}while (true);"});
put("var", new String[]{"Var", "var"});
put("let", new String[]{"Let", "let"});
put("const", new String[]{"Const", "const"});
put("switch", new String[]{"Switch", "switch(expression) {\n" +
" case x:\n" +
" // code block\n" +
" break;\n" +
" case y:\n" +
" // code block\n" +
" break;\n" +
" default:\n" +
" // code block\n" +
" }"});
}};
HashMap<String,String[]> sourceElement =new HashMap<String, String[]>(){{
put("function", new String[]{"Function", "function functionName(){\n\t\n}}"});
put("if", new String[]{"If", "if (true) {\n\t\n}"});
put("try", new String[]{"Try", "try {\n\t\n}}catch(err) {\nconsole.log(err)}"});
然后使用 swith 选择正确的哈希图。
JsonObject mainObj = new JsonObject();
switch(finalScope) {
case "program":
mainObj.add("re",generateArray(program));
break;
case "sourceelement":
mainObj.add("re",generateArray(sourceElement));
同样地。我想知道的是有没有比这更好的方法来完成任务。我正在使用 antlr4 来解析代码。并使用 java 编写我的 lsp