Java Code Examples for org.eclipse.jface.text.ITypedRegion#getLength()
The following examples show how to use
org.eclipse.jface.text.ITypedRegion#getLength() .
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: AbstractLexemeProvider.java From APICloud-Studio with GNU General Public License v3.0 | 6 votes |
/** * Convert the partition that contains the given offset into a list of lexemes. If the includeOffset is not within * the partition found at offset, then the range is extended to include it * * @param document * @param offset * @param includeOffset * @param scanner */ protected AbstractLexemeProvider(IDocument document, int offset, int includeOffset, U scanner) { int start = offset; int end = offset; try { ITypedRegion partition = document.getPartition(offset); start = partition.getOffset(); end = start + partition.getLength(); start = Math.max(0, Math.min(start, includeOffset)); end = Math.min(Math.max(end, includeOffset), document.getLength()); } catch (BadLocationException e) { } this.createLexemeList(document, start, end - start, scanner); }
Example 2
Source File: JavaHeuristicScanner.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override public int nextPosition(int position, boolean forward) { ITypedRegion partition= getPartition(position); if (fPartition.equals(partition.getType())) return super.nextPosition(position, forward); if (forward) { int end= partition.getOffset() + partition.getLength(); if (position < end) return end; } else { int offset= partition.getOffset(); if (position > offset) return offset - 1; } return super.nextPosition(position, forward); }
Example 3
Source File: SseUtilities.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 6 votes |
/** * Finds the partition containing the given offset. * * @param document the document to search * @param offset the offset used to find a matching partition * @return the partition, or null */ public static ITypedRegion getPartition(IStructuredDocument document, int offset) { ITypedRegion[] partitions; try { partitions = TextUtilities.computePartitioning(document, IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, 0, document.getLength(), true); } catch (BadLocationException e) { CorePluginLog.logError(e, "Unexpected bad location exception."); return null; } for (ITypedRegion partition : partitions) { if (partition.getOffset() <= offset && offset < partition.getOffset() + partition.getLength()) { return partition; } } return null; }
Example 4
Source File: JavaHeuristicScanner.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override public int nextPosition(int position, boolean forward) { ITypedRegion partition= getPartition(position); if (fPartition.equals(partition.getType())) return super.nextPosition(position, forward); if (forward) { int end= partition.getOffset() + partition.getLength(); if (position < end) return end; } else { int offset= partition.getOffset(); if (position > offset) return offset - 1; } return super.nextPosition(position, forward); }
Example 5
Source File: PartitionToolkit.java From tlaplus with MIT License | 5 votes |
/** * Prints partition information * @param region * @param document */ public static void printPartition(ITypedRegion region, IDocument document) { if (region == null) { return; } try { StringBuffer messageBuffer = new StringBuffer(); String location = "[" + region.getOffset() + ":" + region.getLength() + "]"; if (region instanceof TLCRegion) { TLCRegion tlcRegion = (TLCRegion) region; messageBuffer.append("TLC:" + tlcRegion.getMessageCode() + " " + tlcRegion.getSeverity() + " " + location); } else { int offset = region.getOffset(); int printLength = Math.min(region.getLength(), 255); String type = region.getType(); Assert.isTrue(type.equals(TagBasedTLCOutputTokenScanner.DEFAULT_CONTENT_TYPE)); String head = document.get(offset, printLength); messageBuffer.append("OUTPUT:" + location + ": >" + head + "< ..."); } TLCUIActivator.getDefault().logDebug(messageBuffer.toString()); } catch (BadLocationException e) { TLCUIActivator.getDefault().logError("Error printing partition", e); } }
Example 6
Source File: ToggleCommentHandler.java From APICloud-Studio with GNU General Public License v3.0 | 5 votes |
private void setTypeContent(StringBuilder buf, ITypedRegion region, String begin, String end) throws BadLocationException { String selectText = iDocument.get(region.getOffset(), region.getLength()); selectText = selectText.substring(begin.length()); selectText = selectText .substring(0, selectText.length() - end.length()); buf.append(selectText); subLength = region.getLength(); lineStartOffset = region.getOffset(); }
Example 7
Source File: JSDocAutoIndentStrategy.java From typescript.java with MIT License | 5 votes |
/** * Guesses if the command operates within a newly created javadoc comment or * not. If in doubt, it will assume that the javadoc is new. * * @param document * the document * @param commandOffset * the command offset * @return <code>true</code> if the comment should be closed, * <code>false</code> if not */ private boolean isNewComment(IDocument document, int commandOffset) { try { int lineIndex = document.getLineOfOffset(commandOffset) + 1; if (lineIndex >= document.getNumberOfLines()) return true; IRegion line = document.getLineInformation(lineIndex); ITypedRegion partition = TextUtilities.getPartition(document, fPartitioning, commandOffset, false); int partitionEnd = partition.getOffset() + partition.getLength(); if (line.getOffset() >= partitionEnd) return false; if (document.getLength() == partitionEnd) return true; // partition goes to end of document - probably a // new comment String comment = document.get(partition.getOffset(), partition.getLength()); if (comment.indexOf("/*", 2) != -1) //$NON-NLS-1$ return true; // enclosed another comment -> probably a new // comment return false; } catch (BadLocationException e) { return false; } }
Example 8
Source File: SupressingMLCommentPredicate.java From n4js with Eclipse Public License 1.0 | 5 votes |
@Override public boolean isInsertClosingBracket(IDocument doc, int offset) throws BadLocationException { if (offset >= 2) { ITypedRegion prevPartition = doc.getPartition(offset - 1); String prevPartitionType = prevPartition.getType(); if (TerminalsTokenTypeToPartitionMapper.SL_COMMENT_PARTITION.equals(prevPartitionType)) { return false; } if (TokenTypeToPartitionMapper.REG_EX_PARTITION.equals(prevPartitionType)) { return prevPartition.getLength() == 1; } } return SingleLineTerminalsStrategy.DEFAULT.isInsertClosingBracket(doc, offset); }
Example 9
Source File: TypedRegionMerger.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
public ITypedRegion[] merge(ITypedRegion[] original) { if (original == null || original.length == 0) return original; ITypedRegion[] result = new ITypedRegion[original.length]; String contentType = original[0].getType(); result[0] = original[0]; for(int i = 1; i < original.length; i++) { ITypedRegion copyMe = original[i]; result[i] = new TypedRegion(copyMe.getOffset(), copyMe.getLength(), contentType); } return result; }
Example 10
Source File: DefaultFoldingRegionProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
/** * @since 2.8 */ protected void computeCommentFolding(IXtextDocument xtextDocument, IFoldingRegionAcceptor<ITextRegion> foldingRegionAcceptor, ITypedRegion typedRegion, boolean initiallyFolded) throws BadLocationException { int offset = typedRegion.getOffset(); int length = typedRegion.getLength(); Matcher matcher = getTextPatternInComment().matcher(xtextDocument.get(offset, length)); if (matcher.find()) { TextRegion significant = new TextRegion(offset + matcher.start(), 0); ((IFoldingRegionAcceptorExtension<ITextRegion>)foldingRegionAcceptor).accept(offset, length, initiallyFolded, significant); } else { ((IFoldingRegionAcceptorExtension<ITextRegion>)foldingRegionAcceptor).accept(offset, length, initiallyFolded); } }
Example 11
Source File: FixedCharCountPartitionDoubleClickSelector.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
@Override protected IRegion getSelectedRegion(IDocument document, ITypedRegion completePartition) throws BadLocationException { if (fLeftBorder == 0 && fRightBorder == 0) return completePartition; if (fRightBorder == -1) { String delimiter = document.getLineDelimiter(document.getLineOfOffset(completePartition.getOffset() + completePartition.getLength() - 1)); if (delimiter == null) fRightBorder = 0; else fRightBorder = delimiter.length(); } return new Region(completePartition.getOffset() + fLeftBorder, completePartition.getLength() - fLeftBorder - fRightBorder); }
Example 12
Source File: ToggleCommentHandler.java From APICloud-Studio with GNU General Public License v3.0 | 4 votes |
private void getTypeContent(int startNum, int endNum, StringBuilder buf, ITextSelection iTextSelection, String type) throws BadLocationException { ITypedRegion typeRegion = iDocument.getPartition(iTextSelection .getOffset()); ITypedRegion typeRegion_new = null; if (iTextSelection.getOffset() > 0) { typeRegion_new = iDocument .getPartition(iTextSelection.getOffset() - 1); } String TYPE_COMMENT = TYPE_HTML_COMMENT; String TYPE_COMMENT_BEGIN = TYPE_HTML_COMMENT_BEGIN; String TYPE_COMMENT_END = TYPE_HTML_COMMENT_END; if (TYPE_CSS.equals(type)) { TYPE_COMMENT = TYPE_CSS_M_COMMENT; TYPE_COMMENT_BEGIN = TYPE_CSS_M_COMMENT_BEGIN; TYPE_COMMENT_END = TYPE_CSS_M_COMMENT_END; } String content; ITypedRegion this_typeRegion = null; if (typeRegion != null && typeRegion.getType().equals(TYPE_COMMENT)) { this_typeRegion = typeRegion; } else if (typeRegion_new != null && typeRegion_new.getType().equals(TYPE_COMMENT)) { this_typeRegion = typeRegion_new; } if (this_typeRegion != null) { content = iDocument.get(this_typeRegion.getOffset(), this_typeRegion.getLength()); content = content.replace(TYPE_COMMENT_BEGIN, ""); content = content.replace(TYPE_COMMENT_END, ""); buf.append(content); lineStartOffset = this_typeRegion.getOffset(); lineEndOffset = (lineStartOffset + this_typeRegion.getLength()); } else { for (int num = startNum; num < endNum + 1; num++) { IRegion information = iDocument.getLineInformation(num); String lineText = iDocument.get(information.getOffset(), information.getLength()); if (num == startNum) { String text = lineText.trim(); String replaceText = TYPE_COMMENT_BEGIN + text; lineText = lineText.replace(text, replaceText); } if (num == endNum) { buf.append(lineText); buf.append(TYPE_COMMENT_END); } else { buf.append(lineText).append(NEWLINE); } } } }
Example 13
Source File: PythonSourceViewer.java From Pydev with Eclipse Public License 1.0 | 4 votes |
/** * Returns a segmentation of the line of the given document appropriate for bidi rendering. The default implementation returns only the string literals of a Java code line as segments. * * @param document the document * @param lineOffset the offset of the line * @return the line's bidi segmentation * @throws BadLocationException in case lineOffset is not valid in document */ protected int[] getBidiLineSegments(int lineOffset) throws BadLocationException { IDocument document = getDocument(); if (document == null) { return null; } IRegion line = document.getLineInformationOfOffset(lineOffset); ITypedRegion[] linePartitioning = document.computePartitioning(lineOffset, line.getLength()); /* * List segmentation= new ArrayList(); for (int i= 0; i < linePartitioning.length; i++) { // if (IJavaPartitions.JAVA_STRING.equals(linePartitioning[i].getType())) // * segmentation.add(linePartitioning[i]); } * * * if (segmentation.size() == 0) return null; */ int size = linePartitioning.length; int[] segments = new int[size * 2 + 1]; int j = 0; for (int i = 0; i < size; i++) { // ITypedRegion segment= (ITypedRegion) segmentation.get(i); ITypedRegion segment = linePartitioning[i]; if (i == 0) { segments[j++] = 0; } int offset = segment.getOffset() - lineOffset; if (offset > segments[j - 1]) { segments[j++] = offset; } if (offset + segment.getLength() >= line.getLength()) { break; } segments[j++] = offset + segment.getLength(); } if (j < segments.length) { int[] result = new int[j]; System.arraycopy(segments, 0, result, 0, j); segments = result; } return segments; }
Example 14
Source File: LineStyleProviderForInlinedCss.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 4 votes |
@Override public boolean prepareRegions(ITypedRegion typedRegion, int lineRequestStart, int lineRequestLength, @SuppressWarnings("rawtypes") Collection holdResults) { ITypedRegion partition = SseUtilities.getPartition(getDocument(), typedRegion.getOffset()); int regionStart = partition.getOffset(); int regionEnd = regionStart + partition.getLength(); List<CSSTextToken> tokens; String content; try { content = getDocument().get(partition.getOffset(), partition.getLength()); } catch (BadLocationException e1) { GWTPluginLog.logWarning(e1, "Unexpected bad location while highlighting a CSS region."); return false; } CssResourceAwareTokenizer t = new CssResourceAwareTokenizer( new StringReader(content)); try { tokens = t.parseText(); } catch (IOException e) { return false; } boolean result = false; if (0 < tokens.size()) { int start = regionStart; int end = start; Iterator<CSSTextToken> i = tokens.iterator(); while (i.hasNext()) { CSSTextToken token = (CSSTextToken) i.next(); end = start + token.length; int styleLength = token.length; /* The token starts in the region */ if (regionStart <= start && start < regionEnd) { /* * [239415] The region may not span the total length of the token - * Adjust the length so that it doesn't overlap with other style * ranges */ if (regionEnd < end) { styleLength = regionEnd - start; } // We should not add style ranges outside the region we have been // called for. if (isContained(typedRegion, start, styleLength)) { addStyleRange(holdResults, getAttributeFor(token.kind), start, styleLength); } } else if (start <= regionStart && regionStart < end) { /* The region starts in the token */ /* The token may not span the total length of the region */ if (end < regionEnd) { styleLength = end - regionStart; } if (isContained(typedRegion, regionStart, styleLength)) { addStyleRange(holdResults, getAttributeFor(token.kind), regionStart, styleLength); } } start += token.length; } result = true; } return result; }
Example 15
Source File: LineStyleProviderForInlinedCss.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 4 votes |
private boolean isContained(ITypedRegion region, int position, int length) { return region.getOffset() <= position && position + length <= region.getOffset() + region.getLength(); }
Example 16
Source File: PropertiesQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean getEscapeUnescapeBackslashProposals(IQuickAssistInvocationContext invocationContext, ArrayList<ICompletionProposal> resultingCollections) throws BadLocationException, BadPartitioningException { ISourceViewer sourceViewer= invocationContext.getSourceViewer(); IDocument document= sourceViewer.getDocument(); Point selectedRange= sourceViewer.getSelectedRange(); int selectionOffset= selectedRange.x; int selectionLength= selectedRange.y; int proposalOffset; int proposalLength; String text; if (selectionLength == 0) { if (selectionOffset != document.getLength()) { char ch= document.getChar(selectionOffset); if (ch == '=' || ch == ':') { //see PropertiesFilePartitionScanner() return false; } } ITypedRegion partition= null; if (document instanceof IDocumentExtension3) partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, invocationContext.getOffset(), false); if (partition == null) return false; String type= partition.getType(); if (!(type.equals(IPropertiesFilePartitions.PROPERTY_VALUE) || type.equals(IDocument.DEFAULT_CONTENT_TYPE))) { return false; } proposalOffset= partition.getOffset(); proposalLength= partition.getLength(); text= document.get(proposalOffset, proposalLength); if (type.equals(IPropertiesFilePartitions.PROPERTY_VALUE)) { text= text.substring(1); //see PropertiesFilePartitionScanner() proposalOffset++; proposalLength--; } } else { proposalOffset= selectionOffset; proposalLength= selectionLength; text= document.get(proposalOffset, proposalLength); } if (PropertiesFileEscapes.containsUnescapedBackslash(text)) { if (resultingCollections == null) return true; resultingCollections.add(new EscapeBackslashCompletionProposal(PropertiesFileEscapes.escape(text, false, true, false), proposalOffset, proposalLength, PropertiesFileEditorMessages.EscapeBackslashCompletionProposal_escapeBackslashes)); return true; } if (PropertiesFileEscapes.containsEscapedBackslashes(text)) { if (resultingCollections == null) return true; resultingCollections.add(new EscapeBackslashCompletionProposal(PropertiesFileEscapes.unescapeBackslashes(text), proposalOffset, proposalLength, PropertiesFileEditorMessages.EscapeBackslashCompletionProposal_unescapeBackslashes)); return true; } return false; }
Example 17
Source File: GWTOpenEditorActionGroup.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 4 votes |
@Override public void run(ITextSelection selection) { IDocument document = editor.getDocumentProvider().getDocument( editor.getEditorInput()); ITypedRegion jsniRegion = JsniParser.getEnclosingJsniRegion(selection, document); if (jsniRegion == null) { // Let the Java Editor do its thing outside of JSNI blocks super.run(selection); return; } ITextSelection jsniBlock = new TextSelection(jsniRegion.getOffset(), jsniRegion.getLength()); // TODO: once we cache Java references in JSNI blocks (for Java search and // refactoring integration) we can just query that to figure out if a Java // reference contains the current selection // Figure out if the selection is inside a Java reference JsniJavaRef javaRef = JsniJavaRef.findEnclosingJavaRef(selection, jsniBlock, document); if (javaRef == null) { return; } IJavaProject project = EditorUtility.getEditorInputJavaElement(editor, false).getJavaProject(); // Finally, try to resolve the Java reference try { IJavaElement element = javaRef.resolveJavaElement(project); // We found a match so delegate to the OpenAction.run(Object[]) method run(new Object[] {element}); } catch (UnresolvedJsniJavaRefException e) { // Could not resolve the Java reference, so do nothing } }
Example 18
Source File: CharacterPairMatcher.java From APICloud-Studio with GNU General Public License v3.0 | 4 votes |
private int searchForward(IDocument doc, int searchStartPosition, char startChar, char endChar, String startPartition) throws BadLocationException { int stack = 0; ITypedRegion[] partitions = computePartitioning(doc, searchStartPosition, doc.getLength() - searchStartPosition); for (ITypedRegion p : partitions) { // skip other partitions that don't match our source partition if (skipPartition(p.getType(), startPartition)) { continue; } // Now search through the partition for the end char int partitionLength = p.getLength(); int partitionEnd = p.getOffset() + partitionLength; int startOffset = Math.max(searchStartPosition, p.getOffset()); int length = partitionEnd - startOffset; String partitionContents = doc.get(startOffset, length); for (int i = 0; i < length; i++) { char c = partitionContents.charAt(i); if (c == endChar) { if (stack == 0) { // it's a match return i + startOffset; } else { // need to close nested pair stack--; } } else if (c == startChar) { // open nested pair stack++; } } } return -1; }
Example 19
Source File: TagUtil.java From APICloud-Studio with GNU General Public License v3.0 | 4 votes |
private static IRegion findMatch(IDocument document, ITypedRegion region, boolean findClose, Collection<String> partitionsToSearch) throws BadLocationException { String tagSrc = document.get(region.getOffset(), region.getLength()); if (tagSrc == null) { return null; } String tagName = getTagName(tagSrc); int start; int length; if (findClose) { // search forwards start = region.getOffset() + region.getLength(); length = document.getLength() - start; } else { // search backwards start = 0; length = region.getOffset() - 1; } if (length < 0) { return null; } List<ITypedRegion> previousPartitions = Arrays.asList(document.computePartitioning(start, length)); if (!findClose) { // search backwards Collections.reverse(previousPartitions); } final String closeTag = "</" + tagName + ">"; //$NON-NLS-1$ //$NON-NLS-2$ final String closeTagWithSpace = "</" + tagName + " "; //$NON-NLS-1$ //$NON-NLS-2$ final String openTag = "<" + tagName + ">"; //$NON-NLS-1$ //$NON-NLS-2$ final String openTagWithSpace = "<" + tagName + " "; //$NON-NLS-1$ //$NON-NLS-2$ // Actually make a "stack" of open and close tags for this tag name and see if it's // unbalanced int stack = 1; for (ITypedRegion pp : previousPartitions) { if (!partitionsToSearch.contains(pp.getType())) { continue; } String src = document.get(pp.getOffset(), pp.getLength()); if (src.startsWith(closeTagWithSpace) || src.startsWith(closeTag)) { // close! if (findClose) { stack--; } else { stack++; } } else if (src.startsWith(openTagWithSpace) || src.startsWith(openTag)) { // open! if (findClose) { stack++; } else { stack--; } } if (stack == 0) { return pp; } } return null; }
Example 20
Source File: TagContentAssistProcessor.java From http4e with Apache License 2.0 | 4 votes |
private TextInfo currentText( IDocument document, int documentOffset){ try { ITypedRegion region = document.getPartition(documentOffset); int partitionOffset = region.getOffset(); int partitionLength = region.getLength(); int index = documentOffset - partitionOffset; String partitionText = document.get(partitionOffset, partitionLength); char c = partitionText.charAt(index); if( Character.isWhitespace(c) || Character.isWhitespace(partitionText.charAt(index - 1))) { return new TextInfo("", documentOffset, true); } else if( c == '<') { return new TextInfo("", documentOffset, true); } else { int start = index; c = partitionText.charAt(start); while (!Character.isWhitespace(c) && c != '<' && start >= 0) { start--; c = partitionText.charAt(start); } start++; int end = index; c = partitionText.charAt(end); while (!Character.isWhitespace(c) && c != '>' && end < partitionLength - 1) { end++; c = partitionText.charAt(end); } String substring = partitionText.substring(start, end); return new TextInfo(substring, partitionOffset + start, false); } } catch (BadLocationException e) { ExceptionHandler.handle(e); } return null; }