@Override public Map<Location, Set<Location>> getReferences() { return indexer.getReferences(); }
@Override public Map<URI, Set<SymbolInformation>> getFileSymbols() { return indexer.getFileSymbols(); }
@Override public void handleFileSaved(URI originalFile, Optional<String> contents) { if (originalSourceToChangedSource.containsKey(originalFile)) { originalSourceToChangedSource.get(originalFile).reload(); resetCompilationUnit(); } }
@Test public void testComputeAllSymbols_interface() throws InterruptedException, ExecutionException, IOException { addFileToFolder(workspaceRoot, "my/folder", "ICoordinates.groovy", "interface ICoordinates {\n" + " abstract double getAt(int idx);\n" + "}\n"); parser.parseAllSymbols(); Map<URI, Set<SymbolInformation>> symbols = parser.getFileSymbols(); // The symbols will contain a lot of inherited and default fields and methods, so we just check to make sure it // contains our custom fields and methods. assertThat(mapHasSymbol(symbols, Optional.absent(), "ICoordinates", SymbolKind.Interface)).isTrue(); assertThat(mapHasSymbol(symbols, Optional.of("ICoordinates"), "getAt", SymbolKind.Method)).isTrue(); assertThat(mapHasSymbol(symbols, Optional.of("getAt"), "idx", SymbolKind.Variable)).isTrue(); }
@Test public void testReferences_typeEnumOneLine() throws IOException { // edge case on one line File enumFile = addFileToFolder(workspaceRoot, "MyEnum.groovy", "enum MyEnum {ONE,TWO}\n"); File scriptFile = addFileToFolder(workspaceRoot, "MyScript.groovy", "MyEnum a\n\n"); parser.parseAllSymbols(); // Find one line enum correctly Set<Location> myEnumExpectedResult = Sets.newHashSet( createLocation(scriptFile.toPath(), Ranges.createRange(0, 0, 0, 6))); assertEquals(myEnumExpectedResult, parser.findReferences(createReferenceParams(enumFile.toURI(), 0, 6, false))); }
@Override public void visitDeclarationExpression(DeclarationExpression expression) { if (expression.getLeftExpression() instanceof Variable) { Variable var = (Variable) expression.getLeftExpression(); SymbolInformation symbol = getVariableSymbolInformation(var); indexer.addSymbol(uri, symbol); if (var.getType() != null && classes.containsKey(var.getType().getName())) { indexer.addReference(classes.get(var.getType().getName()), createLocation(var.getType())); } } super.visitDeclarationExpression(expression); }
@Test public void testCompile_WithJavaExtensionAndGroovySyntax() throws InterruptedException, ExecutionException, IOException { addFileToFolder(root.getRoot(), "ExceptionNewNotSameName.java", "public class ExceptionNew {}"); GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); }
@Test public void testInitialize_absoluteWorkspacePath() throws InterruptedException, ExecutionException { InitializeParams initializeParams = getInitializeParams(Optional.empty()); InitializeResult result = server.initialize(initializeParams).get(); assertInitializeResultIsCorrect(folder.getRoot().toPath().toAbsolutePath().normalize(), result); // Test normalization initializeParams = getInitializeParams(Optional.of( folder.getRoot().toPath().toAbsolutePath().toString() + "/somethingelse/..")); result = server.initialize(initializeParams).get(); assertInitializeResultIsCorrect(folder.getRoot().toPath().toAbsolutePath().normalize(), result); }
@Override public void visitClassExpression(ClassExpression expression) { // Add reference to class if (expression.getType() != null && classes.containsKey(expression.getType().getName())) { indexer.addReference(classes.get(expression.getType().getName()), createLocation(expression)); } super.visitClassExpression(expression); }
@Override public void visitStaticMethodCallExpression(StaticMethodCallExpression expression) { List<MethodNode> possibleMethods = expression.getOwnerType().getMethods(expression.getMethodAsString()); if (!possibleMethods.isEmpty() && expression.getArguments() instanceof ArgumentListExpression) { ArgumentListExpression actualArguments = (ArgumentListExpression) expression.getArguments(); possibleMethods.stream() .filter(method -> areEquals(method.getParameters(), actualArguments)) .forEach(method -> indexer.addReference(createLocation(method), createLocation(expression))); } super.visitStaticMethodCallExpression(expression); }
@Override public CompletionList getCompletion(URI uri, Position position) { return CompletionUtils.createCompletionListFromSymbols(getFileSymbols().get(uri)); }
@Test public void testEmptyWorkspace() throws InterruptedException, ExecutionException, IOException { GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); }
@Before public void before() { server = new GroovyLanguageServer(Mockito.mock(LanguageServerState.class), Mockito.mock(TextDocumentService.class), Mockito.mock(WorkspaceService.class)); }
private GroovyWorkspaceCompiler createGroovyWorkspaceCompiler() { return GroovyWorkspaceCompiler.of(output.getRoot().toPath(), root.getRoot().toPath(), changedOutput.getRoot().toPath()); }
@Test public void testComputeAllSymbols_enum() throws InterruptedException, ExecutionException, IOException { addFileToFolder(workspaceRoot, "Type.groovy", "enum Type {\n" + " ONE, TWO, THREE\n" + "}\n"); parser.parseAllSymbols(); Map<URI, Set<SymbolInformation>> symbols = parser.getFileSymbols(); // The symbols will contain a lot of inherited and default fields and methods, so we just check to make sure it // contains our custom fields and methods. assertThat(mapHasSymbol(symbols, Optional.absent(), "Type", SymbolKind.Enum)).isTrue(); assertThat(mapHasSymbol(symbols, Optional.of("Type"), "ONE", SymbolKind.Field)).isTrue(); assertThat(mapHasSymbol(symbols, Optional.of("Type"), "TWO", SymbolKind.Field)).isTrue(); assertThat(mapHasSymbol(symbols, Optional.of("Type"), "THREE", SymbolKind.Field)).isTrue(); }
@Test public void testCompile_WithJavaExtension() throws InterruptedException, ExecutionException, IOException { addFileToFolder(root.getRoot(), "test1.groovy", "class Coordinates {\n" + " double latitude\n" + " double longitude\n" + " double getAt(int idx) {\n" + " if (idx == 0) latitude\n" + " else if (idx == 1) longitude\n" + " else throw new ExceptionNew(\"Wrong coordinate index, use 0 or 1\")\n" + " }\n" + "}\n"); addFileToFolder(root.getRoot(), "ExceptionNew.java", "public class ExceptionNew {}"); GroovyWorkspaceCompiler compiler = createGroovyWorkspaceCompiler(); assertEquals(NO_ERRORS, compiler.compile(ImmutableSet.of())); }
@Test public void testInitialize_uriWorkspacePath() throws InterruptedException, ExecutionException { InitializeParams params = getInitializeParams(Optional.empty()); InitializeResult result = server.initialize(params).get(); assertInitializeResultIsCorrect(folder.getRoot().toPath().toAbsolutePath().normalize(), result); // Test normalization params = getInitializeParams(Optional.of( folder.getRoot().toPath().toAbsolutePath().toString() + "/somethingelse/..")); result = server.initialize(params).get(); assertInitializeResultIsCorrect(folder.getRoot().toPath().toAbsolutePath().normalize(), result); }
@Override public void visitCatchStatement(CatchStatement statement) { // Add reference to class exception if (classes.containsKey(statement.getExceptionType().getName())) { indexer.addReference(classes.get(statement.getExceptionType().getName()), createLocation(statement.getExceptionType())); } // TODO(#125): add a symbol for the exception variables. Right now statement.getVariable() returns a Parameter // with an invalid location. super.visitCatchStatement(statement); }
@Test public void testComputeAllSymbols_script() throws InterruptedException, ExecutionException, IOException { addFileToFolder(workspaceRoot, "test.groovy", "def name = \"Natacha\"\n" + "def myMethod() {\n" + " def someString = \"Also in symbols\"\n" + " println \"Hello World\"\n" + "}\n" + "println name\n" + "myMethod()\n"); parser.parseAllSymbols(); Map<URI, Set<SymbolInformation>> symbols = parser.getFileSymbols(); assertThat(mapHasSymbol(symbols, Optional.of("test"), "myMethod", SymbolKind.Method)).isTrue(); assertThat(mapHasSymbol(symbols, Optional.of("test"), "name", SymbolKind.Variable)).isTrue(); assertThat(mapHasSymbol(symbols, Optional.of("myMethod"), "someString", SymbolKind.Variable)).isTrue(); }
@Override public void visitConstructorCallExpression(ConstructorCallExpression expression) { if (expression.getType() != null && classes.containsKey(expression.getType().getName())) { indexer.addReference(classes.get(expression.getType().getName()), createLocation(expression)); } super.visitConstructorCallExpression(expression); }