Search in sources :

Example 56 with MalformedTreeException

use of org.eclipse.text.edits.MalformedTreeException in project eclipse.jdt.ls by eclipse.

the class TextEditConverter method visit.

/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.text.edits.TextEditVisitor#visit(org.eclipse.text.edits.
	 * CopyTargetEdit)
	 */
@Override
public boolean visit(CopyTargetEdit edit) {
    try {
        if (edit.getSourceEdit() != null) {
            org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit();
            te.setRange(JDTUtils.toRange(compilationUnit, edit.getOffset(), edit.getLength()));
            Document doc = new Document(compilationUnit.getSource());
            edit.apply(doc, TextEdit.UPDATE_REGIONS);
            String content = doc.get(edit.getSourceEdit().getOffset(), edit.getSourceEdit().getLength());
            if (edit.getSourceEdit().getSourceModifier() != null) {
                content = applySourceModifier(content, edit.getSourceEdit().getSourceModifier());
            }
            te.setNewText(content);
            converted.add(te);
        }
        // do not visit children
        return false;
    } catch (MalformedTreeException | BadLocationException | CoreException e) {
        JavaLanguageServerPlugin.logException("Error converting TextEdits", e);
    }
    return super.visit(edit);
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Document(org.eclipse.jface.text.Document) SimpleDocument(org.eclipse.jdt.internal.core.util.SimpleDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 57 with MalformedTreeException

use of org.eclipse.text.edits.MalformedTreeException in project eclipse.jdt.ls by eclipse.

the class OverrideCompletionProposal method updateReplacementString.

/*
	 * @see JavaTypeCompletionProposal#updateReplacementString(IDocument,char,int,ImportRewrite)
	 */
public String updateReplacementString(IDocument document, int offset, ImportRewrite importRewrite, boolean snippetStringSupport) throws CoreException, BadLocationException {
    Document recoveredDocument = new Document();
    CompilationUnit unit = getRecoveredAST(document, offset, recoveredDocument);
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(unit, offset, importRewrite);
    ITypeBinding declaringType = null;
    ChildListPropertyDescriptor descriptor = null;
    ASTNode node = NodeFinder.perform(unit, offset, 1);
    node = ASTResolving.findParentType(node);
    String result = null;
    if (node instanceof AnonymousClassDeclaration) {
        declaringType = ((AnonymousClassDeclaration) node).resolveBinding();
        descriptor = AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY;
    } else if (node instanceof AbstractTypeDeclaration) {
        AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) node;
        descriptor = declaration.getBodyDeclarationsProperty();
        declaringType = declaration.resolveBinding();
    }
    if (declaringType != null) {
        ASTRewrite rewrite = ASTRewrite.create(unit.getAST());
        IMethodBinding methodToOverride = Bindings.findMethodInHierarchy(declaringType, fMethodName, fParamTypes);
        if (methodToOverride == null && declaringType.isInterface()) {
            // $NON-NLS-1$
            methodToOverride = Bindings.findMethodInType(node.getAST().resolveWellKnownType("java.lang.Object"), fMethodName, fParamTypes);
        }
        if (methodToOverride != null) {
            CodeGenerationSettings settings = PreferenceManager.getCodeGenerationSettings(fCompilationUnit);
            MethodDeclaration stub = StubUtility2Core.createImplementationStubCore(fCompilationUnit, rewrite, importRewrite, context, methodToOverride, declaringType, settings, declaringType.isInterface(), node, snippetStringSupport);
            ListRewrite rewriter = rewrite.getListRewrite(node, descriptor);
            rewriter.insertFirst(stub, null);
            ITrackedNodePosition position = rewrite.track(stub);
            try {
                Map<String, String> options = fCompilationUnit.getOptions(true);
                rewrite.rewriteAST(recoveredDocument, options).apply(recoveredDocument);
                String generatedCode = recoveredDocument.get(position.getStartPosition(), position.getLength());
                String indentAt = getIndentAt(recoveredDocument, position.getStartPosition(), settings);
                int generatedIndent = IndentManipulation.measureIndentUnits(indentAt, settings.tabWidth, settings.indentWidth);
                // Kinda fishy but empirical data shows Override needs to change indent by at
                // least 1
                generatedIndent = Math.max(1, generatedIndent);
                // Cancel generated code indent
                String delimiter = TextUtilities.getDefaultLineDelimiter(document);
                result = IndentManipulation.changeIndent(generatedCode, generatedIndent, settings.tabWidth, settings.indentWidth, "", delimiter);
            } catch (MalformedTreeException | BadLocationException exception) {
                JavaLanguageServerPlugin.logException("Unable to compute override proposal", exception);
            }
        }
    }
    if (result == null) {
        return replacementString;
    }
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ITrackedNodePosition(org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition) BadLocationException(org.eclipse.jface.text.BadLocationException) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)57 BadLocationException (org.eclipse.jface.text.BadLocationException)41 IDocument (org.eclipse.jface.text.IDocument)26 TextEdit (org.eclipse.text.edits.TextEdit)23 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)14 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 Document (org.eclipse.jface.text.Document)13 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)13 CoreException (org.eclipse.core.runtime.CoreException)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)11 Test (org.junit.Test)11 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)10 InsertEdit (org.eclipse.text.edits.InsertEdit)10 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)9 JavaModelException (org.eclipse.jdt.core.JavaModelException)9 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)9 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)8 ArrayList (java.util.ArrayList)7 IPath (org.eclipse.core.runtime.IPath)7