private ITypedRegion getPartition(IDocument doc, int offset) throws BadLocationException { ITypedRegion part = TextUtilities.getPartition( doc, IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, offset, true); if (part.getType() == IDocument.DEFAULT_CONTENT_TYPE && part.getLength() == 0 && offset == doc.getLength() && offset > 0) { // A special case because when cursor at end of document and just after a '=' sign, then we // get a DEFAULT content type // with a empty region. We rather would get the non-empty 'Value' partition just before that // (which has the assignment in it. char assign = doc.getChar(offset - 1); if (isAssign(assign)) { return new TypedRegion(offset - 1, 1, IPropertiesFilePartitions.PROPERTY_VALUE); } else { // For a similar case but where there's extra spaces after the '=' ITypedRegion previousPart = TextUtilities.getPartition( doc, IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, offset - 1, true); int previousEnd = previousPart.getOffset() + previousPart.getLength(); if (previousEnd == offset) { // prefer this over a 0 length partition ending at the same location return previousPart; } } } return part; }
@Override protected int createBackwardBound(final int start) throws BadLocationException { final IPartitionConstraint matcher = getPartitionConstraint(); assert (!matcher.matches(IDocument.DEFAULT_CONTENT_TYPE)); if (matcher.matches(Rweave.R_DEFAULT_CONTENT_TYPE)) { final ITypedRegion cat = Rweave.R_TEX_CAT_UTIL.getCat(fDocument, start); return cat.getOffset(); } final ITypedRegion partition = TextUtilities.getPartition(fDocument, getPartitioning(), start, false); return partition.getOffset(); }
/** * Checks whether <code>position</code> resides in a default (Java) partition of <code>document * </code>. * * @param document the document being modified * @param position the position to be checked * @param partitioning the document partitioning * @return <code>true</code> if <code>position</code> is in the default partition of <code> * document</code>, <code>false</code> otherwise */ private static boolean isDefaultPartition(IDocument document, int position, String partitioning) { Assert.isTrue(position >= 0); Assert.isTrue(position <= document.getLength()); try { ITypedRegion region = TextUtilities.getPartition(document, partitioning, position, false); return region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE); } catch (BadLocationException e) { } return false; }
private ITypedRegion getPartition(IDocument doc, int offset) { ITypedRegion tr = null; // not sure why document would ever be null, but put in this // guard for // https://bugs.eclipse.org/bugs/show_bug.cgi?id=86069 if (doc != null) { try { tr = TextUtilities.getPartition( doc, IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, offset, false); } catch (BadLocationException e) { if (DEBUG) Logger.logException("problem getting partition at: " + offset, e); // $NON-NLS-1$ } } return tr; }
private void AtlStringIndentAfterNewLine(IDocument document, DocumentCommand command) throws BadLocationException { ITypedRegion partition = TextUtilities.getPartition(document, fPartitioning, command.offset, true); int offset = partition.getOffset(); int length = partition.getLength(); if (command.offset == offset + length && document.getChar(offset + length - 1) == '\'') return; String indentation = getLineIndentation(document, command.offset); String delimiter = TextUtilities.getDefaultLineDelimiter(document); IRegion line = document.getLineInformationOfOffset(offset); String string = document.get(line.getOffset(), offset - line.getOffset()); if (string.trim().length() != 0) indentation += String.valueOf("\t\t"); IPreferenceStore preferenceStore = AtlUIPlugin.getDefault().getPreferenceStore(); if (isLineDelimiter(document, command.text)) command.text = "\' +" + command.text + indentation + "\'"; else if (command.text.length() > 1 && preferenceStore.getBoolean(AtlPreferenceConstants.TYPING_ESCAPE_STRINGS)) command.text = getModifiedText(command.text, indentation, delimiter); }
/** * Indents a single line using the java heuristic scanner. Javadoc and multi line comments are * indented as specified by the <code>JavaDocAutoIndentStrategy</code>. * * @param document the document * @param line the line to be indented * @param indenter the java indenter * @param scanner the heuristic scanner * @param commentLines the indent token comment booleans * @param lineIndex the zero-based line index * @return <code>true</code> if the document was modified, <code>false</code> if not * @throws BadLocationException if the document got changed concurrently */ private static boolean indentLine( IDocument document, int line, JavaIndenter indenter, JavaHeuristicScanner scanner, boolean[] commentLines, int lineIndex, int tabSize) throws BadLocationException { IRegion currentLine = document.getLineInformation(line); final int offset = currentLine.getOffset(); int wsStart = offset; // where we start searching for non-WS; after the "//" in single line comments String indent = null; if (offset < document.getLength()) { ITypedRegion partition = TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, offset, true); ITypedRegion startingPartition = TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, offset, false); String type = partition.getType(); if (type.equals(IJavaPartitions.JAVA_DOC) || type.equals(IJavaPartitions.JAVA_MULTI_LINE_COMMENT)) { indent = computeJavadocIndent(document, line, scanner, startingPartition); } else if (!commentLines[lineIndex] && startingPartition.getOffset() == offset && startingPartition.getType().equals(IJavaPartitions.JAVA_SINGLE_LINE_COMMENT)) { return false; } } // standard java indentation if (indent == null) { StringBuffer computed = indenter.computeIndentation(offset); if (computed != null) indent = computed.toString(); else indent = new String(); } // change document: // get current white space int lineLength = currentLine.getLength(); int end = scanner.findNonWhitespaceForwardInAnyPartition(wsStart, offset + lineLength); if (end == JavaHeuristicScanner.NOT_FOUND) end = offset + lineLength; int length = end - offset; String currentIndent = document.get(offset, length); // memorize the fact that a line is a single line comment (but not at column 0) and should be // treated like code // as opposed to commented out code, which should keep its slashes at column 0 if (length > 0) { ITypedRegion partition = TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, end, false); if (partition.getOffset() == end && IJavaPartitions.JAVA_SINGLE_LINE_COMMENT.equals(partition.getType())) { commentLines[lineIndex] = true; } } // only change the document if it is a real change if (!indent.equals(currentIndent)) { document.replace(offset, length, indent); return true; } return false; }
private void smartIndentAfterNewLine(IDocument d, DocumentCommand c) { JavaHeuristicScanner scanner = new JavaHeuristicScanner(d); JavaIndenter indenter = new JavaIndenter(d, scanner, fProject); StringBuffer indent = indenter.computeIndentation(c.offset); if (indent == null) indent = new StringBuffer(); int docLength = d.getLength(); if (c.offset == -1 || docLength == 0) return; try { int p = (c.offset == docLength ? c.offset - 1 : c.offset); int line = d.getLineOfOffset(p); StringBuffer buf = new StringBuffer(c.text + indent); IRegion reg = d.getLineInformation(line); int lineEnd = reg.getOffset() + reg.getLength(); int contentStart = findEndOfWhiteSpace(d, c.offset, lineEnd); c.length = Math.max(contentStart - c.offset, 0); int start = reg.getOffset(); ITypedRegion region = TextUtilities.getPartition(d, fPartitioning, start, true); if (IJavaPartitions.JAVA_DOC.equals(region.getType())) start = d.getLineInformationOfOffset(region.getOffset()).getOffset(); // insert closing brace on new line after an unclosed opening brace if (getBracketCount(d, start, c.offset, true) > 0 && closeBrace() && !isClosed(d, c.offset, c.length)) { c.caretOffset = c.offset + buf.length(); c.shiftsCaret = false; // copy old content of line behind insertion point to new line // unless we think we are inserting an anonymous type definition if (c.offset == 0 || computeAnonymousPosition(d, c.offset - 1, fPartitioning, lineEnd) == -1) { if (lineEnd - contentStart > 0) { c.length = lineEnd - c.offset; buf.append(d.get(contentStart, lineEnd - contentStart).toCharArray()); } } buf.append(TextUtilities.getDefaultLineDelimiter(d)); StringBuffer reference = null; int nonWS = findEndOfWhiteSpace(d, start, lineEnd); if (nonWS < c.offset && d.getChar(nonWS) == '{') reference = new StringBuffer(d.get(start, nonWS - start)); else reference = indenter.getReferenceIndentation(c.offset); if (reference != null) buf.append(reference); buf.append('}'); } // insert extra line upon new line between two braces else if (c.offset > start && contentStart < lineEnd && d.getChar(contentStart) == '}') { int firstCharPos = scanner.findNonWhitespaceBackward(c.offset - 1, start); if (firstCharPos != JavaHeuristicScanner.NOT_FOUND && d.getChar(firstCharPos) == '{') { c.caretOffset = c.offset + buf.length(); c.shiftsCaret = false; StringBuffer reference = null; int nonWS = findEndOfWhiteSpace(d, start, lineEnd); if (nonWS < c.offset && d.getChar(nonWS) == '{') reference = new StringBuffer(d.get(start, nonWS - start)); else reference = indenter.getReferenceIndentation(c.offset); buf.append(TextUtilities.getDefaultLineDelimiter(d)); if (reference != null) buf.append(reference); } } c.text = buf.toString(); } catch (BadLocationException e) { JavaPlugin.log(e); } }