@Override public Void visitImportDirective(DartImportDirective node) { DartLibrary library = compilationUnit.getLibrary(); try { if (Objects.equal(compilationUnit, library.getDefiningCompilationUnit())) { DartImport[] imports = library.getImports(); for (DartImport imprt : imports) { // on URI of library - return defining Unit of imported Library SourceRange uriRange = imprt.getUriRange(); if (SourceRangeUtils.contains(uriRange, startOffset)) { resolvedElement = null; foundElement = imprt.getLibrary().getDefiningCompilationUnit(); wordRegion = new Region(uriRange.getOffset(), uriRange.getLength()); candidateRegion = new Region(0, 0); throw new DartElementFoundException(); } // on #import directive - return DartImport element SourceRange sourceRange = imprt.getSourceRange(); if (SourceRangeUtils.contains(sourceRange, startOffset)) { resolvedElement = null; foundElement = imprt; wordRegion = new Region(sourceRange.getOffset(), sourceRange.getLength()); candidateRegion = new Region(sourceRange.getOffset(), sourceRange.getLength()); throw new DartElementFoundException(); } } } } catch (DartModelException e) { DartCore.logError("Cannot access imports of " + library.getElementName(), e); } return super.visitImportDirective(node); }
@Override public Void visitIdentifier(DartIdentifier node) { if (foundElement == null) { int start = node.getSourceInfo().getOffset(); int length = node.getSourceInfo().getLength(); int end = start + length; if (start <= startOffset && endOffset <= end) { wordRegion = new Region(start, length); Element targetElement = DartAstUtilities.getElement(node, includeDeclarations); if (targetElement == null) { foundElement = null; } else { if (targetElement instanceof VariableElement) { VariableElement variableElement = (VariableElement) targetElement; resolvedElement = variableElement; if (variableElement.getKind() == ElementKind.PARAMETER || variableElement.getKind() == ElementKind.VARIABLE) { foundElement = BindingUtils.getDartElement(compilationUnit.getLibrary(), variableElement); candidateRegion = new Region( variableElement.getNameLocation().getOffset(), variableElement.getNameLocation().getLength()); } else { foundElement = null; } } else { findElementFor(targetElement); // Import prefix is resolved into LibraryElement, so it is correct that corresponding // DartElement is DartLibrary, but this is not what we (and user) wants, because // it looses information. We want DartImport, it gives both DartLibrary and name. if (foundElement instanceof DartLibrary) { try { DartImport[] imports = compilationUnit.getLibrary().getImports(); for (DartImport imprt : imports) { if (Objects.equal(imprt.getLibrary(), foundElement) && Objects.equal(imprt.getPrefix(), node.getName())) { foundElement = imprt; SourceRange range = imprt.getNameRange(); candidateRegion = new Region(range.getOffset(), range.getLength()); } } } catch (DartModelException e) { DartCore.logError("Cannot resolve import " + foundElement.getElementName(), e); } } } } throw new DartElementFoundException(); } } return null; }