Search in sources :

Example 6 with Document

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

the class DelegateCreator method createEdit.

/**
	 * Performs the actual rewriting and adds an edit to the ASTRewrite set with
	 * {@link #setSourceRewrite(CompilationUnitRewrite)}.
	 *
	 * @throws JavaModelException
	 */
public void createEdit() throws JavaModelException {
    try {
        IDocument document = new Document(fDelegateRewrite.getCu().getBuffer().getContents());
        TextEdit edit = fDelegateRewrite.getASTRewrite().rewriteAST(document, fDelegateRewrite.getCu().getJavaProject().getOptions(true));
        edit.apply(document, TextEdit.UPDATE_REGIONS);
        String newSource = Strings.trimIndentation(document.get(fTrackedPosition.getStartPosition(), fTrackedPosition.getLength()), fPreferences.tabWidth, fPreferences.indentWidth, false);
        ASTNode placeholder = fOriginalRewrite.getASTRewrite().createStringPlaceholder(newSource, fDeclaration.getNodeType());
        CategorizedTextEditGroup groupDescription = fOriginalRewrite.createCategorizedGroupDescription(getTextEditGroupLabel(), CATEGORY_DELEGATE);
        ListRewrite bodyDeclarationsListRewrite = fOriginalRewrite.getASTRewrite().getListRewrite(fDeclaration.getParent(), getTypeBodyDeclarationsProperty());
        if (fCopy)
            if (fInsertBefore)
                bodyDeclarationsListRewrite.insertBefore(placeholder, fDeclaration, groupDescription);
            else
                bodyDeclarationsListRewrite.insertAfter(placeholder, fDeclaration, groupDescription);
        else
            bodyDeclarationsListRewrite.replace(fDeclaration, placeholder, groupDescription);
    } catch (BadLocationException e) {
        JavaPlugin.log(e);
    }
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup)

Example 7 with Document

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

the class SourceProvider method initialize.

public void initialize() throws JavaModelException {
    fDocument = fSource == null ? new Document(fTypeRoot.getBuffer().getContents()) : fSource;
    fAnalyzer.initialize();
    if (hasReturnValue()) {
        ASTNode last = getLastStatement();
        if (last != null) {
            ReturnAnalyzer analyzer = new ReturnAnalyzer();
            last.accept(analyzer);
        }
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument)

Example 8 with Document

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

the class CodeAssist method createOrganizeImportOperation.

private OrganizeImportResult createOrganizeImportOperation(ICompilationUnit compilationUnit, List<String> chosen) throws CoreException {
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(compilationUnit.getJavaProject());
    OrganizeImportsOperation operation = new OrganizeImportsOperation(compilationUnit, null, settings.importIgnoreLowercase, !compilationUnit.isWorkingCopy(), true, chosen, null);
    NullProgressMonitor monitor = new NullProgressMonitor();
    TextEdit edit = operation.createTextEdit(monitor);
    OrganizeImportResult result = DtoFactory.newDto(OrganizeImportResult.class);
    TypeNameMatch[][] choices = operation.getChoices();
    //or all conflicts were resolved (!chosen.isEmpty())
    if ((chosen != null && !chosen.isEmpty()) || choices == null || choices.length == 0) {
        IBuffer buffer = compilationUnit.getBuffer();
        IDocument document = new Document(buffer.getContents());
        DocumentChangeListener documentChangeListener = new DocumentChangeListener(document);
        try {
            edit.apply(document);
        } catch (BadLocationException e) {
            LOG.debug("Applying Organize import text edits goes wrong:", e);
        }
        result.setChanges(documentChangeListener.getChanges());
        return result;
    }
    result.setConflicts(createListOfDTOMatches(choices));
    return result;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) OrganizeImportResult(org.eclipse.che.ide.ext.java.shared.dto.OrganizeImportResult) TextEdit(org.eclipse.text.edits.TextEdit) DocumentChangeListener(org.eclipse.jdt.internal.corext.format.DocumentChangeListener) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IBuffer(org.eclipse.jdt.core.IBuffer) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 9 with Document

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

the class MavenServerService method createProblem.

private Problem createProblem(VirtualFileEntry entry, SAXParseException spe) {
    Problem problem = DtoFactory.newDto(Problem.class);
    problem.setError(true);
    problem.setMessage(spe.getMessage());
    if (entry != null) {
        int lineNumber = spe.getLineNumber();
        int columnNumber = spe.getColumnNumber();
        try {
            String content = entry.getVirtualFile().getContentAsString();
            Document document = new Document(content);
            int lineOffset = document.getLineOffset(lineNumber - 1);
            problem.setSourceStart(lineOffset + columnNumber - 1);
            problem.setSourceEnd(lineOffset + columnNumber);
        } catch (ForbiddenException | ServerException | BadLocationException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    return problem;
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) MavenProjectProblem(org.eclipse.che.maven.data.MavenProjectProblem) Problem(org.eclipse.che.ide.ext.java.shared.dto.Problem) Document(org.eclipse.jface.text.Document) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 10 with Document

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

the class JavaDebuggerUtils method findFqnByPosition.

/**
     * Return nested class fqn if line with number {@code lineNumber} contains such element, otherwise return outer class fqn.
     *
     * @param projectPath
     *         project path which contains class with {@code outerClassFqn}
     * @param outerClassFqn
     *         fqn outer class
     * @param lineNumber
     *         line position to search
     * @throws DebuggerException
     */
public String findFqnByPosition(String projectPath, String outerClassFqn, int lineNumber) throws DebuggerException {
    if (projectPath == null) {
        return outerClassFqn;
    }
    IJavaProject project = MODEL.getJavaProject(projectPath);
    IType outerClass;
    IMember iMember;
    try {
        outerClass = project.findType(outerClassFqn);
        if (outerClass == null) {
            return outerClassFqn;
        }
        String source;
        if (outerClass.isBinary()) {
            IClassFile classFile = outerClass.getClassFile();
            source = classFile.getSource();
        } else {
            ICompilationUnit unit = outerClass.getCompilationUnit();
            source = unit.getSource();
        }
        Document document = new Document(source);
        IRegion region = document.getLineInformation(lineNumber);
        int start = region.getOffset();
        int end = start + region.getLength();
        iMember = binSearch(outerClass, start, end);
    } catch (JavaModelException e) {
        throw new DebuggerException(format("Unable to find source for class with fqn '%s' in the project '%s'", outerClassFqn, project), e);
    } catch (BadLocationException e) {
        throw new DebuggerException("Unable to calculate breakpoint location", e);
    }
    if (iMember instanceof IType) {
        return ((IType) iMember).getFullyQualifiedName();
    }
    if (iMember != null) {
        return iMember.getDeclaringType().getFullyQualifiedName();
    }
    return outerClassFqn;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClassFile(org.eclipse.jdt.core.IClassFile) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) Document(org.eclipse.jface.text.Document) IMember(org.eclipse.jdt.core.IMember) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) IType(org.eclipse.jdt.core.IType)

Aggregations

Document (org.eclipse.jface.text.Document)59 IDocument (org.eclipse.jface.text.IDocument)48 BadLocationException (org.eclipse.jface.text.BadLocationException)26 TextEdit (org.eclipse.text.edits.TextEdit)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)9 CoreException (org.eclipse.core.runtime.CoreException)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IRegion (org.eclipse.jface.text.IRegion)8 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)8 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)8 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 TemplateException (org.eclipse.jface.text.templates.TemplateException)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 Template (org.eclipse.jface.text.templates.Template)6 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)6 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)6 InputStream (java.io.InputStream)5 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)5 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)5