Search in sources :

Example 6 with Position

use of org.eclipse.jface.text.Position in project che by eclipse.

the class TaskMarkerProposal method getUpdatedPosition.

private Position getUpdatedPosition(IDocument document) throws BadLocationException {
    IScanner scanner = ToolFactory.createScanner(true, false, false, false);
    scanner.setSource(document.get().toCharArray());
    int token = getSurroundingComment(scanner);
    if (token == ITerminalSymbols.TokenNameEOF) {
        return null;
    }
    int commentStart = scanner.getCurrentTokenStartPosition();
    int commentEnd = scanner.getCurrentTokenEndPosition() + 1;
    int contentStart = commentStart + 2;
    int contentEnd = commentEnd;
    if (token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
        contentStart = commentStart + 3;
        contentEnd = commentEnd - 2;
    } else if (token == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
        contentEnd = commentEnd - 2;
    }
    if (hasContent(document, contentStart, fLocation.getOffset()) || hasContent(document, contentEnd, fLocation.getOffset() + fLocation.getLength())) {
        return new Position(fLocation.getOffset(), fLocation.getLength());
    }
    IRegion startRegion = document.getLineInformationOfOffset(commentStart);
    int start = startRegion.getOffset();
    boolean contentAtBegining = hasContent(document, start, commentStart);
    if (contentAtBegining) {
        start = commentStart;
    }
    int end;
    if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
        if (contentAtBegining) {
            // only to the end of the line
            end = startRegion.getOffset() + startRegion.getLength();
        } else {
            // includes new line
            end = commentEnd;
        }
    } else {
        int endLine = document.getLineOfOffset(commentEnd - 1);
        if (endLine + 1 == document.getNumberOfLines() || contentAtBegining) {
            IRegion endRegion = document.getLineInformation(endLine);
            end = endRegion.getOffset() + endRegion.getLength();
        } else {
            IRegion endRegion = document.getLineInformation(endLine + 1);
            end = endRegion.getOffset();
        }
    }
    if (hasContent(document, commentEnd, end)) {
        end = commentEnd;
        // only remove comment
        start = commentStart;
    }
    return new Position(start, end - start);
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) Position(org.eclipse.jface.text.Position) IRegion(org.eclipse.jface.text.IRegion)

Example 7 with Position

use of org.eclipse.jface.text.Position in project che by eclipse.

the class InclusivePositionUpdater method update.

/*
	 * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
	 */
public void update(DocumentEvent event) {
    int eventOffset = event.getOffset();
    int eventOldLength = event.getLength();
    int eventNewLength = event.getText() == null ? 0 : event.getText().length();
    int deltaLength = eventNewLength - eventOldLength;
    try {
        Position[] positions = event.getDocument().getPositions(fCategory);
        for (int i = 0; i != positions.length; i++) {
            Position position = positions[i];
            if (position.isDeleted())
                continue;
            int offset = position.getOffset();
            int length = position.getLength();
            int end = offset + length;
            if (offset > eventOffset + eventOldLength)
                // position comes way
                // after change - shift
                position.setOffset(offset + deltaLength);
            else if (end < eventOffset) {
            // position comes way before change -
            // leave alone
            } else if (offset <= eventOffset && end >= eventOffset + eventOldLength) {
                // event completely internal to the position - adjust length
                position.setLength(length + deltaLength);
            } else if (offset < eventOffset) {
                // event extends over end of position - adjust length
                int newEnd = eventOffset + eventNewLength;
                position.setLength(newEnd - offset);
            } else if (end > eventOffset + eventOldLength) {
                // event extends from before position into it - adjust offset
                // and length
                // offset becomes end of event, length ajusted acordingly
                // we want to recycle the overlapping part
                position.setOffset(eventOffset);
                int deleted = eventOffset + eventOldLength - offset;
                position.setLength(length - deleted + eventNewLength);
            } else {
                // event consumes the position - delete it
                position.delete();
            }
        }
    } catch (BadPositionCategoryException e) {
    // ignore and return
    }
}
Also used : Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException)

Example 8 with Position

use of org.eclipse.jface.text.Position in project che by eclipse.

the class TemplateEngine method complete.

/**
	 * Inspects the context of the compilation unit around <code>completionPosition</code>
	 * and feeds the collector with proposals.
	 * @param viewer the text viewer
	 * @param completionPosition the context position in the document of the text viewer
	 * @param compilationUnit the compilation unit (may be <code>null</code>)
	 */
public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
    IDocument document = viewer.getDocument();
    if (!(fContextType instanceof CompilationUnitContextType))
        return;
    Point selection = viewer.getSelectedRange();
    Position position = new Position(completionPosition, selection.y);
    // remember selected text
    String selectedText = null;
    if (selection.y != 0) {
        try {
            selectedText = document.get(selection.x, selection.y);
            document.addPosition(position);
            fPositions.put(document, position);
        } catch (BadLocationException e) {
        }
    }
    CompilationUnitContext context = ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit);
    //$NON-NLS-1$
    context.setVariable("selection", selectedText);
    int start = context.getStart();
    int end = context.getEnd();
    IRegion region = new Region(start, end - start);
    Template[] templates = JavaPlugin.getDefault().getTemplateStore().getTemplates();
    if (selection.y == 0) {
        for (int i = 0; i != templates.length; i++) {
            Template template = templates[i];
            if (context.canEvaluate(template)) {
                fProposals.add(new TemplateProposal(template, context, region, getImage()));
            }
        }
    } else {
        if (context.getKey().length() == 0)
            context.setForceEvaluation(true);
        boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
        for (int i = 0; i != templates.length; i++) {
            Template template = templates[i];
            if (context.canEvaluate(template) && (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1))) {
                fProposals.add(new TemplateProposal(templates[i], context, region, getImage()));
            }
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) Point(org.eclipse.swt.graphics.Point) CompilationUnitContextType(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) Template(org.eclipse.jface.text.templates.Template) CompilationUnitContext(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 9 with Position

use of org.eclipse.jface.text.Position in project che by eclipse.

the class ParameterGuessingProposal method guessParameters.

//	/**
//	 * Returns the currently active java editor, or <code>null</code> if it
//	 * cannot be determined.
//	 *
//	 * @return  the currently active java editor, or <code>null</code>
//	 */
//	private JavaEditor getJavaEditor() {
//		IEditorPart part= JavaPlugin.getActivePage().getActiveEditor();
//		if (part instanceof JavaEditor)
//			return (JavaEditor) part;
//		else
//			return null;
//	}
private ICompletionProposal[][] guessParameters(char[][] parameterNames) throws JavaModelException {
    // find matches in reverse order.  Do this because people tend to declare the variable meant for the last
    // parameter last.  That is, local variables for the last parameter in the method completion are more
    // likely to be closer to the point of code completion. As an example consider a "delegation" completion:
    //
    // 		public void myMethod(int param1, int param2, int param3) {
    // 			someOtherObject.yourMethod(param1, param2, param3);
    //		}
    //
    // The other consideration is giving preference to variables that have not previously been used in this
    // code completion (which avoids "someOtherObject.yourMethod(param1, param1, param1)";
    int count = parameterNames.length;
    fPositions = new Position[count];
    fChoices = new ICompletionProposal[count][];
    String[] parameterTypes = getParameterTypes();
    ParameterGuesser guesser = new ParameterGuesser(getEnclosingElement());
    IJavaElement[][] assignableElements = getAssignableElements();
    for (int i = count - 1; i >= 0; i--) {
        String paramName = new String(parameterNames[i]);
        Position position = new Position(0, 0);
        boolean isLastParameter = i == count - 1;
        ICompletionProposal[] argumentProposals = guesser.parameterProposals(parameterTypes[i], paramName, position, assignableElements[i], fFillBestGuess, isLastParameter);
        if (argumentProposals.length == 0) {
            JavaCompletionProposal proposal = new JavaCompletionProposal(paramName, 0, paramName.length(), null, paramName, 0);
            if (isLastParameter)
                proposal.setTriggerCharacters(new char[] { ',' });
            argumentProposals = new ICompletionProposal[] { proposal };
        }
        fPositions[i] = position;
        fChoices[i] = argumentProposals;
    }
    return fChoices;
}
Also used : Position(org.eclipse.jface.text.Position) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) Point(org.eclipse.swt.graphics.Point)

Example 10 with Position

use of org.eclipse.jface.text.Position in project che by eclipse.

the class ParameterGuessingProposal method computeGuessingCompletion.

/**
	 * Creates the completion string. Offsets and Lengths are set to the offsets and lengths of the
	 * parameters.
	 *
	 * @return the completion string
	 * @throws org.eclipse.jdt.core.JavaModelException if parameter guessing failed
	 */
private String computeGuessingCompletion() throws JavaModelException {
    StringBuffer buffer = new StringBuffer();
    appendMethodNameReplacement(buffer);
    FormatterPrefs prefs = getFormatterPrefs();
    setCursorPosition(buffer.length());
    if (prefs.afterOpeningParen)
        buffer.append(SPACE);
    char[][] parameterNames = fProposal.findParameterNames(null);
    fChoices = guessParameters(parameterNames);
    int count = fChoices.length;
    int replacementOffset = getReplacementOffset();
    for (int i = 0; i < count; i++) {
        if (i != 0) {
            if (prefs.beforeComma)
                buffer.append(SPACE);
            buffer.append(COMMA);
            if (prefs.afterComma)
                buffer.append(SPACE);
        }
        ICompletionProposal proposal = fChoices[i][0];
        String argument = proposal.getDisplayString();
        Position position = fPositions[i];
        position.setOffset(replacementOffset + buffer.length());
        position.setLength(argument.length());
        if (// handle the "unknown" case where we only insert a proposal.
        proposal instanceof JavaCompletionProposal)
            ((JavaCompletionProposal) proposal).setReplacementOffset(replacementOffset + buffer.length());
        buffer.append(argument);
    }
    if (prefs.beforeClosingParen)
        buffer.append(SPACE);
    buffer.append(RPAREN);
    if (canAutomaticallyAppendSemicolon())
        buffer.append(SEMICOLON);
    return buffer.toString();
}
Also used : Position(org.eclipse.jface.text.Position) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) Point(org.eclipse.swt.graphics.Point)

Aggregations

Position (org.eclipse.jface.text.Position)421 BadLocationException (org.eclipse.jface.text.BadLocationException)145 Annotation (org.eclipse.jface.text.source.Annotation)110 IDocument (org.eclipse.jface.text.IDocument)80 ArrayList (java.util.ArrayList)75 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)57 IRegion (org.eclipse.jface.text.IRegion)55 Test (org.junit.Test)54 HashMap (java.util.HashMap)49 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)46 Point (org.eclipse.swt.graphics.Point)40 List (java.util.List)39 Iterator (java.util.Iterator)31 TypedPosition (org.eclipse.jface.text.TypedPosition)31 Region (org.eclipse.jface.text.Region)27 IFile (org.eclipse.core.resources.IFile)22 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)21 ProjectionAnnotation (org.eclipse.jface.text.source.projection.ProjectionAnnotation)21 Map (java.util.Map)15 CoreException (org.eclipse.core.runtime.CoreException)15