org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner Java Examples
The following examples show how to use
org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner.
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: IndentUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Indents the line range specified by <code>lines</code> in * <code>document</code>. The passed Java project may be * <code>null</code>, it is used solely to obtain formatter preferences. * * @param document the document to be changed * @param lines the line range to be indented * @param project the Java project to get the formatter preferences from, or * <code>null</code> if global preferences should be used * @param result the result from a previous call to <code>indentLines</code>, * in order to maintain comment line properties, or <code>null</code>. * Note that the passed result may be changed by the call. * @return an indent result that may be queried for changes and can be * reused in subsequent indentation operations * @throws BadLocationException if <code>lines</code> is not a valid line * range on <code>document</code> */ public static IndentResult indentLines(IDocument document, ILineRange lines, IJavaProject project, IndentResult result) throws BadLocationException { int numberOfLines= lines.getNumberOfLines(); if (numberOfLines < 1) return new IndentResult(null); result= reuseOrCreateToken(result, numberOfLines); JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); JavaIndenter indenter= new JavaIndenter(document, scanner, project); boolean changed= false; int tabSize= CodeFormatterUtil.getTabWidth(project); for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++) { changed |= indentLine(document, line, indenter, scanner, result.commentLinesAtColumnZero, i++, tabSize); } result.hasChanged= changed; return result; }
Example #2
Source File: JavaAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Returns the block balance, i.e. zero if the blocks are balanced at <code>offset</code>, a * negative number if there are more closing than opening braces, and a positive number if there * are more opening than closing braces. * * @param document the document * @param offset the offset * @param partitioning the partitioning * @return the block balance */ private static int getBlockBalance(IDocument document, int offset, String partitioning) { if (offset < 1) return -1; if (offset >= document.getLength()) return 1; int begin= offset; int end= offset - 1; JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); while (true) { begin= scanner.findOpeningPeer(begin - 1, '{', '}'); end= scanner.findClosingPeer(end + 1, '{', '}'); if (begin == -1 && end == -1) return 0; if (begin == -1) return -1; if (end == -1) return 1; } }
Example #3
Source File: JavaAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static CompilationUnitInfo getCompilationUnitForMethod(IDocument document, int offset) { try { JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); IRegion sourceRange= scanner.findSurroundingBlock(offset); if (sourceRange == null) return null; String source= document.get(sourceRange.getOffset(), sourceRange.getLength()); StringBuffer contents= new StringBuffer(); contents.append("class ____C{void ____m()"); //$NON-NLS-1$ final int methodOffset= contents.length(); contents.append(source); contents.append('}'); char[] buffer= contents.toString().toCharArray(); return new CompilationUnitInfo(buffer, sourceRange.getOffset() - methodOffset); } catch (BadLocationException e) { JavaPlugin.log(e); } return null; }
Example #4
Source File: JavaCompletionProposalComputer.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
protected final int guessMethodContextInformationPosition(ContentAssistInvocationContext context) { final int contextPosition= context.getInvocationOffset(); IDocument document= context.getDocument(); JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); int bound= Math.max(-1, contextPosition - 2000); // try the innermost scope of parentheses that looks like a method call int pos= contextPosition - 1; do { int paren= scanner.findOpeningPeer(pos, bound, '(', ')'); if (paren == JavaHeuristicScanner.NOT_FOUND) break; int token= scanner.previousToken(paren - 1, bound); // next token must be a method name (identifier) or the closing angle of a // constructor call of a parameterized type. if (token == Symbols.TokenIDENT || token == Symbols.TokenGREATERTHAN) return paren + 1; pos= paren - 1; } while (true); return contextPosition; }
Example #5
Source File: JavaTypeCompletionProposalComputer.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override protected int guessContextInformationPosition(ContentAssistInvocationContext context) { final int contextPosition= context.getInvocationOffset(); IDocument document= context.getDocument(); JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); int bound= Math.max(-1, contextPosition - 200); // try the innermost scope of angle brackets that looks like a generic type argument list try { int pos= contextPosition - 1; do { int angle= scanner.findOpeningPeer(pos, bound, '<', '>'); if (angle == JavaHeuristicScanner.NOT_FOUND) break; int token= scanner.previousToken(angle - 1, bound); // next token must be a method name that is a generic type if (token == Symbols.TokenIDENT) { int off= scanner.getPosition() + 1; int end= angle; String ident= document.get(off, end - off).trim(); if (JavaHeuristicScanner.isGenericStarter(ident)) return angle + 1; } pos= angle - 1; } while (true); } catch (BadLocationException x) { } return super.guessContextInformationPosition(context); }
Example #6
Source File: JavaAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Skips the scope opened by <code>token</code>. * * @param scanner the scanner * @param start the start position * @param token the token * @return the position after the scope or <code>JavaHeuristicScanner.NOT_FOUND</code> */ private static int skipScope(JavaHeuristicScanner scanner, int start, int token) { int openToken= token; int closeToken; switch (token) { case Symbols.TokenLPAREN: closeToken= Symbols.TokenRPAREN; break; case Symbols.TokenLBRACKET: closeToken= Symbols.TokenRBRACKET; break; case Symbols.TokenLBRACE: closeToken= Symbols.TokenRBRACE; break; default: Assert.isTrue(false); return -1; // dummy } int depth= 1; int p= start; while (true) { int tok= scanner.nextToken(p, JavaHeuristicScanner.UNBOUND); p= scanner.getPosition(); if (tok == openToken) { depth++; } else if (tok == closeToken) { depth--; if (depth == 0) return p + 1; } else if (tok == Symbols.TokenEOF) { return JavaHeuristicScanner.NOT_FOUND; } } }
Example #7
Source File: JavaAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Finds a closing parenthesis to the left of <code>position</code> in document, where that parenthesis is only * separated by whitespace from <code>position</code>. If no such parenthesis can be found, <code>position</code> is returned. * * @param scanner the java heuristic scanner set up on the document * @param position the first character position in <code>document</code> to be considered * @return the position of a closing parenthesis left to <code>position</code> separated only by whitespace, or <code>position</code> if no parenthesis can be found */ private static int findClosingParenToLeft(JavaHeuristicScanner scanner, int position) { if (position < 1) return position; if (scanner.previousToken(position - 1, JavaHeuristicScanner.UNBOUND) == Symbols.TokenRPAREN) return scanner.getPosition() + 1; return position; }
Example #8
Source File: JavaAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void smartIndentAfterClosingBracket(IDocument d, DocumentCommand c) { if (c.offset == -1 || d.getLength() == 0) return; try { int p= (c.offset == d.getLength() ? c.offset - 1 : c.offset); int line= d.getLineOfOffset(p); int start= d.getLineOffset(line); int whiteend= findEndOfWhiteSpace(d, start, c.offset); JavaHeuristicScanner scanner= new JavaHeuristicScanner(d); JavaIndenter indenter= new JavaIndenter(d, scanner, fProject); // shift only when line does not contain any text up to the closing bracket if (whiteend == c.offset) { // evaluate the line with the opening bracket that matches out closing bracket int reference= indenter.findReferencePosition(c.offset, false, true, false, false); int indLine= d.getLineOfOffset(reference); if (indLine != -1 && indLine != line) { // take the indent of the found line StringBuffer replaceText= new StringBuffer(getIndentOfLine(d, indLine)); // add the rest of the current line including the just added close bracket replaceText.append(d.get(whiteend, c.offset - whiteend)); replaceText.append(c.text); // modify document command c.length += c.offset - start; c.offset= start; c.text= replaceText.toString(); } } } catch (BadLocationException e) { JavaPlugin.log(e); } }
Example #9
Source File: IndentAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Indents a single line using the java heuristic scanner. Javadoc and multiline comments are * indented as specified by the <code>JavaDocAutoIndentStrategy</code>. * * @param document the document * @param line the line to be indented * @param caret the caret position * @param indenter the java indenter * @param scanner the heuristic scanner * @param multiLine <code>true</code> if more than one line is being indented * @return <code>true</code> if <code>document</code> was modified, <code>false</code> otherwise * @throws BadLocationException if the document got changed concurrently */ private boolean indentLine(IDocument document, int line, int caret, JavaIndenter indenter, JavaHeuristicScanner scanner, boolean multiLine) throws BadLocationException { IJavaProject project= getJavaProject(); ReplaceData data= computeReplaceData(document, line, indenter, scanner, multiLine, fIsTabAction, project); String indent= data.indent; int end= data.end; int offset= data.offset; int length= end - offset; String currentIndent= document.get(offset, length); // if we are right before the text start / line end, and already after the insertion point // then just insert a tab. if (fIsTabAction && caret == end && whiteSpaceLength(currentIndent, project) >= whiteSpaceLength(indent, project)) { String tab= getTabEquivalent(project); document.replace(caret, 0, tab); fCaretOffset= caret + tab.length(); return true; } // set the caret offset so it can be used when setting the selection if (caret >= offset && caret <= end) fCaretOffset= offset + indent.length(); else fCaretOffset= -1; // only change the document if it is a real change if (!indent.equals(currentIndent)) { document.replace(offset, length, indent); return true; } else return false; }
Example #10
Source File: JavaParameterListValidator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private boolean checkGenericsHeuristic(IDocument document, int end, int bound) { JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); return scanner.looksLikeClassInstanceCreationBackward(end, bound); }
Example #11
Source File: IndentAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Computes and returns the indentation for a javadoc line. The line * must be inside a javadoc comment. * * @param document the document * @param line the line in document * @param scanner the scanner * @param partition the javadoc partition * @return the indent, or <code>null</code> if not computable * @throws BadLocationException * @since 3.1 */ private static String computeJavadocIndent(IDocument document, int line, JavaHeuristicScanner scanner, ITypedRegion partition) throws BadLocationException { if (line == 0) // impossible - the first line is never inside a javadoc comment return null; // don't make any assumptions if the line does not start with \s*\* - it might be // commented out code, for which we don't want to change the indent final IRegion lineInfo= document.getLineInformation(line); final int lineStart= lineInfo.getOffset(); final int lineLength= lineInfo.getLength(); final int lineEnd= lineStart + lineLength; int nonWS= scanner.findNonWhitespaceForwardInAnyPartition(lineStart, lineEnd); if (nonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(nonWS) != '*') { if (nonWS == JavaHeuristicScanner.NOT_FOUND) return document.get(lineStart, lineLength); return document.get(lineStart, nonWS - lineStart); } // take the indent from the previous line and reuse IRegion previousLine= document.getLineInformation(line - 1); int previousLineStart= previousLine.getOffset(); int previousLineLength= previousLine.getLength(); int previousLineEnd= previousLineStart + previousLineLength; StringBuffer buf= new StringBuffer(); int previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd); if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(previousLineNonWS) != '*') { // align with the comment start if the previous line is not an asterisked line previousLine= document.getLineInformationOfOffset(partition.getOffset()); previousLineStart= previousLine.getOffset(); previousLineLength= previousLine.getLength(); previousLineEnd= previousLineStart + previousLineLength; previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd); if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND) previousLineNonWS= previousLineEnd; // add the initial space // TODO this may be controlled by a formatter preference in the future buf.append(' '); } String indentation= document.get(previousLineStart, previousLineNonWS - previousLineStart); buf.insert(0, indentation); return buf.toString(); }
Example #12
Source File: JavaAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private void smartIndentAfterOpeningBracket(IDocument d, DocumentCommand c) { if (c.offset < 1 || d.getLength() == 0) return; JavaHeuristicScanner scanner= new JavaHeuristicScanner(d); int p= (c.offset == d.getLength() ? c.offset - 1 : c.offset); try { // current line int line= d.getLineOfOffset(p); int lineOffset= d.getLineOffset(line); // make sure we don't have any leading comments etc. if (d.get(lineOffset, p - lineOffset).trim().length() != 0) return; // line of last Java code int pos= scanner.findNonWhitespaceBackward(p, JavaHeuristicScanner.UNBOUND); if (pos == -1) return; int lastLine= d.getLineOfOffset(pos); // only shift if the last java line is further up and is a braceless block candidate if (lastLine < line) { JavaIndenter indenter= new JavaIndenter(d, scanner, fProject); StringBuffer indent= indenter.computeIndentation(p, true); String toDelete= d.get(lineOffset, c.offset - lineOffset); if (indent != null && !indent.toString().equals(toDelete)) { c.text= indent.append(c.text).toString(); c.length += c.offset - lineOffset; c.offset= lineOffset; } } } catch (BadLocationException e) { JavaPlugin.log(e); } }
Example #13
Source File: JavaAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Checks if it is required to copy the old content of the line after caret position to new line * before inserting the closing brace. * * @param document the document being modified * @param offset the offset of the caret position, relative to the line start. * @param partitioning the document partitioning * @param max the max position * @return <code>true</code> if the old content of the line after caret position has to be * copied to new line before inserting the closing brace, <code>false</code> otherwise */ private boolean copyContent(IDocument document, int offset, String partitioning, int max) { // find the opening parenthesis for every closing parenthesis on the current line after offset // return true if it looks like a method declaration // or an expression for an if, while, for, catch statement JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); int pos= offset; int length= max; int scanTo= scanner.scanForward(pos, length, '}'); if (scanTo == -1) scanTo= length; int closingParen= findClosingParenToLeft(scanner, pos) - 1; int openingParen= -1; while (true) { int startScan= closingParen + 1; closingParen= scanner.scanForward(startScan, scanTo, ')'); if (closingParen == -1) { if (openingParen != -1) return false; try { int p= findEndOfWhiteSpace(document, pos, scanTo); char ch= document.getChar(p); if (ch == ',' || ch == ';') return false; } catch (BadLocationException e) { // ignore } break; } openingParen= scanner.findOpeningPeer(closingParen - 1, '(', ')'); // no way an expression at the beginning of the document can mean anything if (openingParen < 1) break; // only select insert positions for parenthesis currently embracing the caret if (openingParen > pos) { openingParen= -1; continue; } if (looksLikeAnonymousClassDef(document, partitioning, scanner, openingParen - 1)) return false; } return true; }
Example #14
Source File: JavaAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private int getPeerPosition(IDocument document, DocumentCommand command) { if (document.getLength() == 0) return 0; /* * Search for scope closers in the pasted text and find their opening peers * in the document. */ Document pasted= new Document(command.text); installJavaStuff(pasted); int firstPeer= command.offset; JavaHeuristicScanner pScanner= new JavaHeuristicScanner(pasted); JavaHeuristicScanner dScanner= new JavaHeuristicScanner(document); // add scope relevant after context to peer search int afterToken= dScanner.nextToken(command.offset + command.length, JavaHeuristicScanner.UNBOUND); try { switch (afterToken) { case Symbols.TokenRBRACE: pasted.replace(pasted.getLength(), 0, "}"); //$NON-NLS-1$ break; case Symbols.TokenRPAREN: pasted.replace(pasted.getLength(), 0, ")"); //$NON-NLS-1$ break; case Symbols.TokenRBRACKET: pasted.replace(pasted.getLength(), 0, "]"); //$NON-NLS-1$ break; } } catch (BadLocationException e) { // cannot happen Assert.isTrue(false); } int pPos= 0; // paste text position (increasing from 0) int dPos= Math.max(0, command.offset - 1); // document position (decreasing from paste offset) while (true) { int token= pScanner.nextToken(pPos, JavaHeuristicScanner.UNBOUND); pPos= pScanner.getPosition(); switch (token) { case Symbols.TokenLBRACE: case Symbols.TokenLBRACKET: case Symbols.TokenLPAREN: pPos= skipScope(pScanner, pPos, token); if (pPos == JavaHeuristicScanner.NOT_FOUND) return firstPeer; break; // closed scope -> keep searching case Symbols.TokenRBRACE: int peer= dScanner.findOpeningPeer(dPos, '{', '}'); dPos= peer - 1; if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer; firstPeer= peer; break; // keep searching case Symbols.TokenRBRACKET: peer= dScanner.findOpeningPeer(dPos, '[', ']'); dPos= peer - 1; if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer; firstPeer= peer; break; // keep searching case Symbols.TokenRPAREN: peer= dScanner.findOpeningPeer(dPos, '(', ')'); dPos= peer - 1; if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer; firstPeer= peer; break; // keep searching case Symbols.TokenCASE: case Symbols.TokenDEFAULT: JavaIndenter indenter= new JavaIndenter(document, dScanner, fProject); peer= indenter.findReferencePosition(dPos, false, false, false, true); if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer; firstPeer= peer; break; // keep searching case Symbols.TokenEOF: return firstPeer; default: // keep searching } } }
Example #15
Source File: IndentAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Indent the given <code>document</code> based on the <code>project</code> settings and * return a text edit describing the changes applied to the document. Returns <b>null</b> * if no changes have been applied. * <p> * WARNING: This method does change the content of the given document. * </p> * <p> * This method is for internal use only, it should not be called. * </p> * * @param document the document to indent must have a java partitioning installed * @param project the project to retrieve the indentation settings from, <b>null</b> for workspace settings * @return a text edit describing the changes or <b>null</b> if no changes required * @throws BadLocationException if the document got modified concurrently * * @since 3.4 */ public static TextEdit indent(IDocument document, IJavaProject project) throws BadLocationException { int offset= 0; int length= document.getLength(); JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); JavaIndenter indenter= new JavaIndenter(document, scanner, project); ArrayList<ReplaceEdit> edits= new ArrayList<ReplaceEdit>(); int firstLine= document.getLineOfOffset(offset); // check for marginal (zero-length) lines int minusOne= length == 0 ? 0 : 1; int numberOfLines= document.getLineOfOffset(offset + length - minusOne) - firstLine + 1; int shift= 0; for (int i= 0; i < numberOfLines; i++) { ReplaceData data= computeReplaceData(document, firstLine + i, indenter, scanner, numberOfLines > 1, false, project); int replaceLength= data.end - data.offset; String currentIndent= document.get(data.offset, replaceLength); // only change the document if it is a real change if (!data.indent.equals(currentIndent)) { edits.add(new ReplaceEdit(data.offset + shift, replaceLength, data.indent)); //We need to change the document, the indenter depends on it. document.replace(data.offset, replaceLength, data.indent); shift-= data.indent.length() - replaceLength; } } if (edits.size() == 0) return null; if (edits.size() == 1) return edits.get(0); MultiTextEdit result= new MultiTextEdit(); for (Iterator<ReplaceEdit> iterator= edits.iterator(); iterator.hasNext();) { TextEdit edit= iterator.next(); result.addChild(edit); } return result; }
Example #16
Source File: IndentUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Computes and returns the indentation for a javadoc line. The line * must be inside a javadoc comment. * * @param document the document * @param line the line in document * @param scanner the scanner * @param partition the comment partition * @return the indent, or <code>null</code> if not computable * @throws BadLocationException */ private static String computeJavadocIndent(IDocument document, int line, JavaHeuristicScanner scanner, ITypedRegion partition) throws BadLocationException { if (line == 0) // impossible - the first line is never inside a javadoc comment return null; // don't make any assumptions if the line does not start with \s*\* - it might be // commented out code, for which we don't want to change the indent final IRegion lineInfo= document.getLineInformation(line); final int lineStart= lineInfo.getOffset(); final int lineLength= lineInfo.getLength(); final int lineEnd= lineStart + lineLength; int nonWS= scanner.findNonWhitespaceForwardInAnyPartition(lineStart, lineEnd); if (nonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(nonWS) != '*') { if (nonWS == JavaHeuristicScanner.NOT_FOUND) return document.get(lineStart, lineLength); return document.get(lineStart, nonWS - lineStart); } // take the indent from the previous line and reuse IRegion previousLine= document.getLineInformation(line - 1); int previousLineStart= previousLine.getOffset(); int previousLineLength= previousLine.getLength(); int previousLineEnd= previousLineStart + previousLineLength; StringBuffer buf= new StringBuffer(); int previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd); if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND || document.getChar(previousLineNonWS) != '*') { // align with the comment start if the previous line is not an asterix line previousLine= document.getLineInformationOfOffset(partition.getOffset()); previousLineStart= previousLine.getOffset(); previousLineLength= previousLine.getLength(); previousLineEnd= previousLineStart + previousLineLength; previousLineNonWS= scanner.findNonWhitespaceForwardInAnyPartition(previousLineStart, previousLineEnd); if (previousLineNonWS == JavaHeuristicScanner.NOT_FOUND) previousLineNonWS= previousLineEnd; // add the initial space // TODO this may be controlled by a formatter preference in the future buf.append(' '); } String indentation= document.get(previousLineStart, previousLineNonWS - previousLineStart); buf.insert(0, indentation); return buf.toString(); }
Example #17
Source File: IndentUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Shifts the line range specified by <code>lines</code> in * <code>document</code>. The amount that the lines get shifted * are determined by the first line in the range, all subsequent * lines are adjusted accordingly. The passed Java project may be * <code>null</code>, it is used solely to obtain formatter * preferences. * * @param document the document to be changed * @param lines the line range to be shifted * @param project the Java project to get the formatter preferences * from, or <code>null</code> if global preferences should * be used * @param result the result from a previous call to * <code>shiftLines</code>, in order to maintain comment * line properties, or <code>null</code>. Note that the * passed result may be changed by the call. * @return an indent result that may be queried for changes and can * be reused in subsequent indentation operations * @throws BadLocationException if <code>lines</code> is not a * valid line range on <code>document</code> */ public static IndentResult shiftLines(IDocument document, ILineRange lines, IJavaProject project, IndentResult result) throws BadLocationException { int numberOfLines= lines.getNumberOfLines(); if (numberOfLines < 1) return new IndentResult(null); result= reuseOrCreateToken(result, numberOfLines); result.hasChanged= false; JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); JavaIndenter indenter= new JavaIndenter(document, scanner, project); String current= getCurrentIndent(document, lines.getStartLine()); StringBuffer correct= indenter.computeIndentation(document.getLineOffset(lines.getStartLine())); if (correct == null) return result; // bail out int tabSize= CodeFormatterUtil.getTabWidth(project); StringBuffer addition= new StringBuffer(); int difference= subtractIndent(correct, current, addition, tabSize); if (difference == 0) return result; if (result.leftmostLine == -1) result.leftmostLine= getLeftMostLine(document, lines, tabSize); int maxReduction= computeVisualLength(getCurrentIndent(document, result.leftmostLine + lines.getStartLine()), tabSize); if (difference > 0) { for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++) addIndent(document, line, addition, result.commentLinesAtColumnZero, i++); } else { int reduction= Math.min(-difference, maxReduction); for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++) cutIndent(document, line, reduction, tabSize, result.commentLinesAtColumnZero, i++); } result.hasChanged= true; return result; }
Example #18
Source File: ParameterContextInformation.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
protected boolean checkGenericsHeuristic(IDocument document, int end, int bound) { JavaHeuristicScanner scanner = new JavaHeuristicScanner(document); return scanner.looksLikeClassInstanceCreationBackward(end, bound); }
Example #19
Source File: JavaAutoIndentStrategy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 3 votes |
/** * Checks whether the content of <code>document</code> at <code>position</code> looks like an * anonymous class definition. <code>position</code> must be to the left of the opening * parenthesis of the definition's parameter list. * * @param document the document being modified * @param partitioning the document partitioning * @param scanner the scanner * @param position the first character position in <code>document</code> to be considered * @return <code>true</code> if the content of <code>document</code> looks like an anonymous class definition, <code>false</code> otherwise */ private static boolean looksLikeAnonymousClassDef(IDocument document, String partitioning, JavaHeuristicScanner scanner, int position) { int previousCommaParenEqual= scanner.scanBackward(position - 1, JavaHeuristicScanner.UNBOUND, new char[] {',', '(', '='}); if (previousCommaParenEqual == -1 || position < previousCommaParenEqual + 5) // 2 for borders, 3 for "new" return false; if (isNewMatch(document, previousCommaParenEqual + 1, position - previousCommaParenEqual - 2, partitioning)) return true; return false; }