Search in sources :

Example 16 with IRegion

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

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

the class MethodDeclarationCompletionProposal method updateReplacementString.

/* (non-Javadoc)
     * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
     */
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject());
    boolean addComments = settings.createComments;
    String[] empty = new String[0];
    String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
    String declTypeName = fType.getTypeQualifiedName('.');
    boolean isInterface = fType.isInterface();
    StringBuffer buf = new StringBuffer();
    if (addComments) {
        String comment = CodeGeneration.getMethodComment(fType.getCompilationUnit(), declTypeName, fMethodName, empty, empty, fReturnTypeSig, empty, null, lineDelim);
        if (comment != null) {
            buf.append(comment);
            buf.append(lineDelim);
        }
    }
    if (fReturnTypeSig != null) {
        if (!isInterface) {
            //$NON-NLS-1$
            buf.append("private ");
        }
    } else {
        if (fType.isEnum())
            //$NON-NLS-1$
            buf.append("private ");
        else
            //$NON-NLS-1$
            buf.append("public ");
    }
    if (fReturnTypeSig != null) {
        buf.append(Signature.toString(fReturnTypeSig));
    }
    buf.append(' ');
    buf.append(fMethodName);
    if (isInterface) {
        //$NON-NLS-1$
        buf.append("();");
        buf.append(lineDelim);
    } else {
        //$NON-NLS-1$
        buf.append("() {");
        buf.append(lineDelim);
        String body = CodeGeneration.getMethodBodyContent(fType.getCompilationUnit(), declTypeName, fMethodName, fReturnTypeSig == null, "", //$NON-NLS-1$
        lineDelim);
        if (body != null) {
            buf.append(body);
            buf.append(lineDelim);
        }
        //$NON-NLS-1$
        buf.append("}");
        buf.append(lineDelim);
    }
    String stub = buf.toString();
    // use the code formatter
    IRegion region = document.getLineInformationOfOffset(getReplacementOffset());
    int lineStart = region.getOffset();
    int indent = Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth);
    String replacement = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, lineDelim, fType.getJavaProject());
    if (replacement.endsWith(lineDelim)) {
        replacement = replacement.substring(0, replacement.length() - lineDelim.length());
    }
    setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement));
    return true;
}
Also used : CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) StyledString(org.eclipse.jface.viewers.StyledString) IRegion(org.eclipse.jface.text.IRegion)

Example 18 with IRegion

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

the class CodeTemplateContext method changeLineDelimiter.

private static String changeLineDelimiter(String code, String lineDelim) {
    try {
        ILineTracker tracker = new DefaultLineTracker();
        tracker.set(code);
        int nLines = tracker.getNumberOfLines();
        if (nLines == 1) {
            return code;
        }
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < nLines; i++) {
            if (i != 0) {
                buf.append(lineDelim);
            }
            IRegion region = tracker.getLineInformation(i);
            String line = code.substring(region.getOffset(), region.getOffset() + region.getLength());
            buf.append(line);
        }
        return buf.toString();
    } catch (BadLocationException e) {
        // can not happen
        return code;
    }
}
Also used : DefaultLineTracker(org.eclipse.jface.text.DefaultLineTracker) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) ILineTracker(org.eclipse.jface.text.ILineTracker)

Example 19 with IRegion

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

the class JavaContext method getIndentation.

/**
	 * Returns the indentation level at the position of code completion.
	 *
	 * @return the indentation level at the position of the code completion
	 */
private int getIndentation() {
    int start = getStart();
    IDocument document = getDocument();
    try {
        IRegion region = document.getLineInformationOfOffset(start);
        String lineContent = document.get(region.getOffset(), region.getLength());
        IJavaProject project = getJavaProject();
        return Strings.computeIndentUnits(lineContent, project);
    } catch (BadLocationException e) {
        return 0;
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 20 with IRegion

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

the class IndentUtil method addIndent.

/**
	 * Indents line <code>line</code> in <code>document</code> with <code>indent</code>.
	 * Leaves leading comment signs alone.
	 *
	 * @param document the document
	 * @param line the line
	 * @param indent the indentation to insert
	 * @param commentlines
	 * @throws BadLocationException on concurrent document modification
	 */
private static void addIndent(IDocument document, int line, CharSequence indent, boolean[] commentlines, int relative) throws BadLocationException {
    IRegion region = document.getLineInformation(line);
    int insert = region.getOffset();
    int endOffset = region.getOffset() + region.getLength();
    // go behind line comments
    if (!commentlines[relative]) {
        while (insert < endOffset - 2 && document.get(insert, 2).equals(SLASHES)) insert += 2;
    }
    // insert indent
    document.replace(insert, 0, indent.toString());
}
Also used : IRegion(org.eclipse.jface.text.IRegion)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)668 BadLocationException (org.eclipse.jface.text.BadLocationException)291 Region (org.eclipse.jface.text.Region)265 IDocument (org.eclipse.jface.text.IDocument)143 Test (org.junit.Test)108 Point (org.eclipse.swt.graphics.Point)76 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)57 ITypedRegion (org.eclipse.jface.text.ITypedRegion)55 Position (org.eclipse.jface.text.Position)50 ArrayList (java.util.ArrayList)37 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)35 IFile (org.eclipse.core.resources.IFile)33 ITextSelection (org.eclipse.jface.text.ITextSelection)32 IEditorPart (org.eclipse.ui.IEditorPart)29 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)24 StyledText (org.eclipse.swt.custom.StyledText)22 CoreException (org.eclipse.core.runtime.CoreException)19 List (java.util.List)18 Document (org.eclipse.jface.text.Document)17 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)17