use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class HierarchyInformationPresenter method modelRange2WidgetRange.
// copied from org.eclipse.jface.text.information.InformationPresenter due to visibility problems
protected IRegion modelRange2WidgetRange(IRegion region) {
if (sourceViewer instanceof ITextViewerExtension5) {
ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
return extension.modelRange2WidgetRange(region);
}
IRegion visibleRegion = sourceViewer.getVisibleRegion();
int start = region.getOffset() - visibleRegion.getOffset();
int end = start + region.getLength();
if (end > visibleRegion.getLength())
end = visibleRegion.getLength();
return new Region(start, end - start);
}
use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class ToggleSLCommentAction method isBlockCommented.
/**
* Determines whether each line is prefixed by one of the prefixes.
*
* @param startLine Start line in document
* @param endLine End line in document
* @param prefixes Possible comment prefixes
* @param document The document
* @return <code>true</code> iff each line from <code>startLine</code>
* to and including <code>endLine</code> is prepended by one
* of the <code>prefixes</code>, ignoring whitespace at the
* begin of line
* @since 2.1
*/
protected boolean isBlockCommented(int startLine, int endLine, String[] prefixes, IDocument document) {
try {
// check for occurrences of prefixes in the given lines
for (int i = startLine; i <= endLine; i++) {
IRegion line = document.getLineInformation(i);
String text = document.get(line.getOffset(), line.getLength());
int[] found = TextUtilities.indexOf(prefixes, text, 0);
if (found[0] == -1)
// found a line which is not commented
return false;
String s = document.get(line.getOffset(), found[0]);
s = s.trim();
if (s.length() != 0)
// found a line which is not commented
return false;
}
return true;
} catch (BadLocationException x) {
// should not happen
log.error(x.getMessage(), x);
}
return false;
}
use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class ToggleSLCommentAction method isSelectionCommented.
/**
* Is the given selection single-line commented?
*
* @param selection Selection to check
* @return <code>true</code> iff all selected lines are commented
* @since 2.1
*/
protected boolean isSelectionCommented(ISelection selection) {
if (!(selection instanceof ITextSelection))
return false;
ITextSelection textSelection = (ITextSelection) selection;
if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0)
return false;
IDocument document = getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
try {
IRegion block = getTextBlockFromSelection(textSelection, document);
ITypedRegion[] regions = TextUtilities.computePartitioning(document, fDocumentPartitioning, block.getOffset(), block.getLength(), false);
// [startline, endline, startline, endline, ...]
int[] lines = new int[regions.length * 2];
for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
// start line of region
lines[j] = getFirstCompleteLineOfRegion(regions[i], document);
// end line of region
int length = regions[i].getLength();
int offset = regions[i].getOffset() + length;
if (length > 0)
offset--;
lines[j + 1] = (lines[j] == -1 ? -1 : document.getLineOfOffset(offset));
}
// Perform the check
for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
String[] prefixes = fPrefixesMap.get(regions[i].getType());
if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0)
if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document))
return false;
}
return true;
} catch (BadLocationException x) {
// should not happen
log.error(x.getMessage(), x);
}
return false;
}
use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class XtextTemplateContext method createTemplateTranslator.
protected TemplateTranslator createTemplateTranslator() {
try {
int offset = getStart();
IRegion lineRegion = getDocument().getLineInformationOfOffset(offset);
String line = getDocument().get(lineRegion.getOffset(), lineRegion.getLength());
int i = 0;
while (i < line.length() && Character.isWhitespace(line.charAt(i))) {
i++;
}
if (i != 0)
return new IndentationAwareTemplateTranslator(line.substring(0, i));
return new TemplateTranslator();
} catch (BadLocationException ex) {
return new TemplateTranslator();
}
}
use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class DefaultFoldingRegionProviderTest method testSignificantPartOfMlComment_01.
@Test
public void testSignificantPartOfMlComment_01() throws Exception {
IFile iFile = createFile("foo/bar.foldingtestlanguage", "\n/**\n *\n */\n element foo end");
IXtextDocument document = openFileAndReturnDocument(iFile);
DefaultFoldingRegionProvider reg = createFoldingRegionProvider();
Collection<FoldedPosition> regions = reg.getFoldingRegions(document);
assertEquals(1, regions.size());
FoldedPosition position = Iterables.getOnlyElement(regions);
assertEquals(1, position.getOffset());
assertEquals("/**\n *\n */\n".length(), position.getLength());
assertEquals(0, position.computeCaptionOffset(document));
IRegion[] projectionRegions = position.computeProjectionRegions(document);
assertEquals(1, projectionRegions.length);
assertEquals("\n/**\n".length(), projectionRegions[0].getOffset());
assertEquals(" *\n */\n".length(), projectionRegions[0].getLength());
}
Aggregations