Java Code Examples for org.eclipse.jdt.core.ITypeRoot#getSourceRange()

The following examples show how to use org.eclipse.jdt.core.ITypeRoot#getSourceRange() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FindOccurrencesEngine.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public String run(ITypeRoot input, int offset, int length) throws JavaModelException {
	if (input.getSourceRange() == null) {
		return SearchMessages.FindOccurrencesEngine_noSource_text;
	}

	final CompilationUnit root= SharedASTProvider.getAST(input, SharedASTProvider.WAIT_YES, null);
	if (root == null) {
		return SearchMessages.FindOccurrencesEngine_cannotParse_text;
	}
	return run(root, offset, length);
}
 
Example 2
Source File: StructureSelectionAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final  void run() {
	IJavaElement inputElement= EditorUtility.getEditorInputJavaElement(fEditor, false);
	if (!(inputElement instanceof ITypeRoot && inputElement.exists()))
		return;

	ITypeRoot typeRoot= (ITypeRoot) inputElement;
	ISourceRange sourceRange;
	try {
		sourceRange= typeRoot.getSourceRange();
		if (sourceRange == null || sourceRange.getLength() == 0) {
			MessageDialog.openInformation(fEditor.getEditorSite().getShell(),
				SelectionActionMessages.StructureSelect_error_title,
				SelectionActionMessages.StructureSelect_error_message);
			return;
		}
	} catch (JavaModelException e) {
	}
	ITextSelection selection= getTextSelection();
	ISourceRange newRange= getNewSelectionRange(createSourceRange(selection), typeRoot);
	// Check if new selection differs from current selection
	if (selection.getOffset() == newRange.getOffset() && selection.getLength() == newRange.getLength())
		return;
	fSelectionHistory.remember(new SourceRange(selection.getOffset(), selection.getLength()));
	try {
		fSelectionHistory.ignoreSelectionChanges();
		fEditor.selectAndReveal(newRange.getOffset(), newRange.getLength());
	} finally {
		fSelectionHistory.listenToSelectionChanges();
	}
}
 
Example 3
Source File: FoldingRangeHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot unit, IProgressMonitor monitor) {
	try {
		ISourceRange range = unit.getSourceRange();
		if (!SourceRange.isAvailable(range)) {
			return;
		}

		String contents = unit.getSource();
		if (StringUtils.isBlank(contents)) {
			return;
		}

		final int shift = range.getOffset();
		IScanner scanner = getScanner();
		scanner.setSource(contents.toCharArray());
		scanner.resetTo(shift, shift + range.getLength());

		int start = shift;
		int token = 0;
		Stack<Integer> regionStarts = new Stack<>();
		while (token != ITerminalSymbols.TokenNameEOF) {
			start = scanner.getCurrentTokenStartPosition();
			switch (token) {
				case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
				case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
					int end = scanner.getCurrentTokenEndPosition();
					FoldingRange commentFoldingRange = new FoldingRange(scanner.getLineNumber(start) - 1, scanner.getLineNumber(end) - 1);
					commentFoldingRange.setKind(FoldingRangeKind.Comment);
					foldingRanges.add(commentFoldingRange);
					break;
				case ITerminalSymbols.TokenNameCOMMENT_LINE:
					String currentSource = String.valueOf(scanner.getCurrentTokenSource());
					if (REGION_START_PATTERN.matcher(currentSource).lookingAt()) {
						regionStarts.push(start);
					} else if (REGION_END_PATTERN.matcher(currentSource).lookingAt()) {
						if (regionStarts.size() > 0) {
							FoldingRange regionFolding = new FoldingRange(scanner.getLineNumber(regionStarts.pop()) - 1, scanner.getLineNumber(start) - 1);
							regionFolding.setKind(FoldingRangeKind.Region);
							foldingRanges.add(regionFolding);
						}
					}
					break;
				default:
					break;
			}
			token = getNextToken(scanner);
		}
		computeTypeRootRanges(foldingRanges, unit, scanner);
	} catch (CoreException e) {
		JavaLanguageServerPlugin.logException("Problem with folding range for " + unit.getPath().toPortableString(), e);
		monitor.setCanceled(true);
	}
}