/** Test for {@link DartFunction#getVisibleRange()} for local functions. */ public void test_getVisibleRange() throws Exception { TestProject testProject = new TestProject("Test"); try { CompilationUnit unit = testProject.setUnitContent( "Test.dart", Joiner.on("\n") .join( "// filler filler filler filler filler filler filler filler filler filler", "f() {", " v1() {};", " {", " v2() {};", " }//marker1", " v3() {};", "}//marker2", "")); String source = unit.getSource(); DartFunction globalFunction = (DartFunction) unit.getChildren()[0]; DartElement[] functions = globalFunction.getChildren(); assertThat(functions).hasSize(3); // v1 { DartFunction f = (DartFunction) functions[0]; SourceRange range = f.getVisibleRange(); assertEquals("v1", f.getElementName()); assertEquals(source.indexOf("v1"), range.getOffset()); assertEquals(source.indexOf("marker2") - 1, range.getOffset() + range.getLength()); } // v2 { DartFunction f = (DartFunction) functions[1]; SourceRange range = f.getVisibleRange(); assertEquals("v2", f.getElementName()); assertEquals(source.indexOf("v2"), range.getOffset()); assertEquals(source.indexOf("marker1") - 1, range.getOffset() + range.getLength()); } // v3 { DartFunction f = (DartFunction) functions[2]; SourceRange range = f.getVisibleRange(); assertEquals("v3", f.getElementName()); assertEquals(source.indexOf("v3"), range.getOffset()); assertEquals(source.indexOf("marker2") - 1, range.getOffset() + range.getLength()); } } finally { testProject.dispose(); } }
/** Selects a Dart Element in an editor */ public static void revealInEditor(IEditorPart part, Object element) { if (element == null) { return; } // TODO(scheglov) to be removed with method // if (part instanceof DartEditor) { // ((DartEditor) part).setSelection(element); // return; // } // Support for non-Dart editor try { SourceRange range = null; DartX.todo(); if (element instanceof SourceReference) { range = ((SourceReference) element).getSourceRange(); } if (range != null) { revealInEditor(part, range.getOffset(), range.getLength()); } } catch (DartModelException e) { // don't reveal } }
private final void processNode(SemanticToken token, DartNode node) { token.update(node); token.attachSource(fSourceViewer.getDocument()); // sometimes node has wrong source range; log problem if (token.getSource() == null) { IDocument document = fSourceViewer.getDocument(); if (node != null && document != null) { SourceInfo sourceInfo = node.getSourceInfo(); if (sourceInfo != null) { DartToolsPlugin.log( "Bad node: " + node.getClass().getName() + " offset:" + sourceInfo.getOffset() + " len:" + sourceInfo.getLength() + " in document: " + document.getLength()); } } return; } // try SemanticHighlighting instances for (int i = 0, n = fJobSemanticHighlightings.length; i < n; i++) { if (fJobHighlightings[i].isEnabled()) { SemanticHighlighting semanticHighlighting = fJobSemanticHighlightings[i]; // try multiple positions { List<SourceRange> ranges = semanticHighlighting.consumesMulti(token); if (ranges != null) { for (SourceRange range : ranges) { int offset = range.getOffset(); int length = range.getLength(); if (offset > -1 && length > 0) { fCollector.addPosition(offset, length, fJobHighlightings[i]); } } break; } } // try single position boolean consumes; if (node instanceof DartIdentifier) { consumes = semanticHighlighting.consumesIdentifier(token); } else { consumes = semanticHighlighting.consumes(token); } if (consumes) { int offset = node.getSourceInfo().getOffset(); int length = node.getSourceInfo().getLength(); if (offset > -1 && length > 0) { fCollector.addPosition(offset, length, fJobHighlightings[i]); } break; } } } token.clear(); }
@Override public void matchFound(SearchMatch match) { DartElement element = match.getElement(); SourceRange range = match.getSourceRange(); search.addMatch( new DartElementMatch( element, 0 /* match.getRule() */, range.getOffset(), range.getLength(), match.getQuality().ordinal() /* accuracy */, false /* isReadAccess */, false /* isWriteAccess */, false, false)); }
/** Selects and reveals the given source range in the given editor part. */ public static void revealInEditor( IEditorPart part, com.google.dart.engine.utilities.source.SourceRange range) { if (part != null && range != null) { revealInEditor(part, range.getOffset(), range.getLength()); } }