Search in sources :

Example 21 with IDocument

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

the class TextFileBufferManager method createEmptyDocument.

/*
	 * @see org.eclipse.core.filebuffers.ITextFileBufferManager#createEmptyDocument(org.eclipse.core.runtime.IPath, org.eclipse.core.filebuffers.LocationKind)
	 * @since 3.3
	 */
public IDocument createEmptyDocument(final IPath location, final LocationKind locationKind) {
    //		IDocument documentFromFactory= createDocumentFromFactory(location, locationKind);
    final IDocument document;
    //		if (documentFromFactory != null)
    //			document= documentFromFactory;
    //		else
    document = new SynchronizableDocument();
    if (location == null)
        return document;
    // Set the initial line delimiter
    if (document instanceof IDocumentExtension4) {
        String initalLineDelimiter = getLineDelimiterPreference(location, locationKind);
        if (initalLineDelimiter != null)
            ((IDocumentExtension4) document).setInitialLineDelimiter(initalLineDelimiter);
    }
    return document;
}
Also used : IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) IDocument(org.eclipse.jface.text.IDocument)

Example 22 with IDocument

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

the class AbstractJavaCompletionProposal method apply.

/*
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension1#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
	 */
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    IDocument document = viewer.getDocument();
    if (fTextViewer == null)
        fTextViewer = viewer;
    // don't apply the proposal if for some reason we're not valid any longer
    if (!isInJavadoc() && !validate(document, offset, null)) {
        setCursorPosition(offset);
        if (trigger != '\0') {
            try {
                document.replace(offset, 0, String.valueOf(trigger));
                setCursorPosition(getCursorPosition() + 1);
                if (trigger == '(' && autocloseBrackets()) {
                    //$NON-NLS-1$
                    document.replace(getReplacementOffset() + getCursorPosition(), 0, ")");
                //						setUpLinkedMode(document, ')');
                }
            } catch (BadLocationException x) {
            // ignore
            }
        }
        return;
    }
    // don't eat if not in preferences, XOR with Ctrl
    // but: if there is a selection, replace it!
    Point selection = viewer.getSelectedRange();
    fToggleEating = (stateMask & SWT.CTRL) != 0;
    int newLength = selection.x + selection.y - getReplacementOffset();
    if ((insertCompletion() ^ fToggleEating) && newLength >= 0)
        setReplacementLength(newLength);
    apply(document, trigger, offset);
    fToggleEating = false;
}
Also used : Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) Point(org.eclipse.swt.graphics.Point)

Example 23 with IDocument

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

the class JavaCompletionProposalComputer method guessMethodContextInformationPosition.

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);
        // constructor call of a parameterized type.
        if (token == Symbols.TokenIDENT || token == Symbols.TokenGREATERTHAN)
            return paren + 1;
        pos = paren - 1;
    } while (true);
    return contextPosition;
}
Also used : JavaHeuristicScanner(org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner) Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument)

Example 24 with IDocument

use of org.eclipse.jface.text.IDocument 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 25 with IDocument

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

the class TemplateProposal method apply.

/*
     * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
     */
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    IDocument document = viewer.getDocument();
    try {
        fContext.setReadOnly(false);
        int start;
        TemplateBuffer templateBuffer;
        try {
            beginCompoundChange(viewer);
            int oldReplaceOffset = getReplaceOffset();
            try {
                // this may already modify the document (e.g. add imports)
                templateBuffer = fContext.evaluate(fTemplate);
            } catch (TemplateException e1) {
                fSelectedRegion = fRegion;
                return;
            }
            start = getReplaceOffset();
            int shift = start - oldReplaceOffset;
            int end = Math.max(getReplaceEndOffset(), offset + shift);
            // insert template string
            if (end > document.getLength())
                end = offset;
            String templateString = templateBuffer.getString();
            document.replace(start, end - start, templateString);
        } finally {
            endCompoundChange(viewer);
        }
        // translate positions
        LinkedModeModelImpl model = new LinkedModeModelImpl();
        TemplateVariable[] variables = templateBuffer.getVariables();
        MultiVariableGuess guess = fContext instanceof CompilationUnitContext ? ((CompilationUnitContext) fContext).getMultiVariableGuess() : null;
        boolean hasPositions = false;
        for (int i = 0; i != variables.length; i++) {
            TemplateVariable variable = variables[i];
            if (variable.isUnambiguous())
                continue;
            LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
            int[] offsets = variable.getOffsets();
            int length = variable.getLength();
            LinkedPosition first;
            if (guess != null && variable instanceof MultiVariable) {
                first = new VariablePosition(document, offsets[0] + start, length, guess, (MultiVariable) variable);
                guess.addSlave((VariablePosition) first);
            } else {
                String[] values = variable.getValues();
                ICompletionProposal[] proposals = new ICompletionProposal[values.length];
                for (int j = 0; j < values.length; j++) {
                    //						ensurePositionCategoryInstalled(document, model);
                    Position pos = new Position(offsets[0] + start, length);
                    //						document.addPosition(getCategory(), pos);
                    proposals[j] = new PositionBasedCompletionProposal(values[j], pos, length);
                }
                if (proposals.length > 1)
                    first = new ProposalPosition(document, offsets[0] + start, length, proposals);
                else
                    first = new LinkedPosition(document, offsets[0] + start, length);
            }
            for (int j = 0; j != offsets.length; j++) if (j == 0) {
                if (first instanceof ProposalPosition) {
                    RegionImpl region = new RegionImpl();
                    region.setLength(first.getLength());
                    region.setOffset(first.getOffset());
                    LinkedDataImpl data = new LinkedDataImpl();
                    ICompletionProposal[] choices = ((ProposalPosition) first).getChoices();
                    if (choices != null) {
                        for (ICompletionProposal choice : choices) {
                            data.addValues(choice.getDisplayString());
                        }
                        group.setData(data);
                    }
                    group.addPositions(region);
                } else {
                    RegionImpl region = new RegionImpl();
                    region.setLength(first.getLength());
                    region.setOffset(first.getOffset());
                    group.addPositions(region);
                }
            } else {
                RegionImpl region = new RegionImpl();
                region.setLength(length);
                region.setOffset(offsets[j] + start);
                group.addPositions(region);
            }
            model.addGroups(group);
            hasPositions = true;
        }
        if (hasPositions) {
            model.setEscapePosition(getCaretOffset(templateBuffer) + start);
            this.linkedModeModel = model;
            //				model.forceInstall();
            //				JavaEditor editor= getJavaEditor();
            //				if (editor != null) {
            //					model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
            //				}
            //
            //				LinkedModeUI ui= new EditorLinkedModeUI(model, viewer);
            //				ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
            //				ui.enter();
            //ui.getSelectedRegion();
            fSelectedRegion = fRegion;
        } else {
            fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
        }
    } catch (BadLocationException e) {
        JavaPlugin.log(e);
        //			openErrorDialog(viewer.getTextWidget().getShell(), e);
        fSelectedRegion = fRegion;
    }
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) StyledString(org.eclipse.jface.viewers.StyledString) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) TemplateException(org.eclipse.jface.text.templates.TemplateException) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) ProposalPosition(org.eclipse.che.jface.text.link.ProposalPosition) Position(org.eclipse.jface.text.Position) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) LinkedDataImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl) Point(org.eclipse.swt.graphics.Point) ProposalPosition(org.eclipse.che.jface.text.link.ProposalPosition) CompilationUnitContext(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IDocument (org.eclipse.jface.text.IDocument)1055 BadLocationException (org.eclipse.jface.text.BadLocationException)379 Document (org.eclipse.jface.text.Document)189 IRegion (org.eclipse.jface.text.IRegion)150 Test (org.junit.Test)107 CoreException (org.eclipse.core.runtime.CoreException)102 Point (org.eclipse.swt.graphics.Point)94 IFile (org.eclipse.core.resources.IFile)90 ArrayList (java.util.ArrayList)85 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)84 Position (org.eclipse.jface.text.Position)74 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)74 IEditorPart (org.eclipse.ui.IEditorPart)62 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)54 IPath (org.eclipse.core.runtime.IPath)54 ITextSelection (org.eclipse.jface.text.ITextSelection)53 TextEdit (org.eclipse.text.edits.TextEdit)49 Region (org.eclipse.jface.text.Region)47 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)44 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)41