Search in sources :

Example 61 with IDocument

use of org.eclipse.jface.text.IDocument in project tdi-studio-se by Talend.

the class JavaProcessor method formatCode.

/**
     * DOC nrousseau Comment method "formatCode".
     *
     * from SourceViewer.doOperation for FORMAT.
     *
     * @param processCode
     * @return
     */
@SuppressWarnings({ "unchecked" })
private String formatCode(String processCode) {
    // we cannot make calls to Ui in headless mode
    if (ProcessorUtilities.isExportConfig() || CommonsPlugin.isHeadless()) {
        // nothing to do
        return processCode;
    }
    final IDocument document = new Document(processCode);
    JavaTextTools tools = JavaPlugin.getDefault().getJavaTextTools();
    tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
    IFormattingContext context = null;
    DocumentRewriteSession rewriteSession = null;
    if (document instanceof IDocumentExtension4) {
        rewriteSession = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL);
    }
    try {
        final String rememberedContents = document.get();
        try {
            final MultiPassContentFormatter formatter = new MultiPassContentFormatter(IJavaPartitions.JAVA_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE);
            formatter.setMasterStrategy(new JavaFormattingStrategy());
            // formatter.setSlaveStrategy(new CommentFormattingStrategy(),
            // IJavaPartitions.JAVA_DOC);
            // formatter.setSlaveStrategy(new CommentFormattingStrategy(),
            // IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
            // formatter.setSlaveStrategy(new CommentFormattingStrategy(),
            // IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
            context = new FormattingContext();
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
            Map<String, String> preferences;
            if (this.getTalendJavaProject() == null) {
                preferences = new HashMap<String, String>(JavaCore.getOptions());
            } else {
                // use project options
                preferences = new HashMap<String, String>(this.getTalendJavaProject().getJavaProject().getOptions(true));
            }
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, preferences);
            formatter.format(document, context);
        } catch (RuntimeException x) {
            // fire wall for
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=47472
            // if something went wrong we undo the changes we just did
            // TODO to be removed after 3.0 M8
            document.set(rememberedContents);
            throw x;
        }
    } finally {
        if (rewriteSession != null && document instanceof IDocumentExtension4) {
            ((IDocumentExtension4) document).stopRewriteSession(rewriteSession);
        }
        if (context != null) {
            context.dispose();
        }
    }
    return document.get();
}
Also used : IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) FormattingContext(org.eclipse.jface.text.formatter.FormattingContext) IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) JavaTextTools(org.eclipse.jdt.ui.text.JavaTextTools) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) MultiPassContentFormatter(org.eclipse.jface.text.formatter.MultiPassContentFormatter) JavaFormattingStrategy(org.eclipse.jdt.internal.ui.text.java.JavaFormattingStrategy) IDocument(org.eclipse.jface.text.IDocument)

Example 62 with IDocument

use of org.eclipse.jface.text.IDocument in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonDocumentProvider method createDocument.

@Override
protected IDocument createDocument(Object element) throws CoreException {
    IDocument document = super.createDocument(element);
    if (document != null) {
        JsonScanner scanner = new JsonScanner(new ColorManager(), store);
        Set<String> tokens = YAMLToken.VALID_TOKENS.keySet();
        FastPartitioner partitioner = new FastPartitioner(scanner, tokens.toArray(new String[tokens.size()]));
        document.setDocumentPartitioner(partitioner);
        partitioner.connect(document);
    }
    return document;
}
Also used : FastPartitioner(org.eclipse.jface.text.rules.FastPartitioner) ColorManager(org.dadacoalition.yedit.editor.ColorManager) IDocument(org.eclipse.jface.text.IDocument)

Example 63 with IDocument

use of org.eclipse.jface.text.IDocument in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonEditor method validate.

private void validate(boolean onOpen) {
    IEditorInput editorInput = getEditorInput();
    final IDocument document = getDocumentProvider().getDocument(getEditorInput());
    // such files.
    if (!(editorInput instanceof IFileEditorInput)) {
        YEditLog.logError("Marking errors not supported for files outside of a project.");
        YEditLog.logger.info("editorInput is not a part of a project.");
        return;
    }
    if (document instanceof JsonDocument) {
        final IFileEditorInput fileEditorInput = (IFileEditorInput) editorInput;
        final IFile file = fileEditorInput.getFile();
        if (onOpen) {
            // force parsing of yaml to init parsing errors
            ((JsonDocument) document).onChange();
        }
        clearMarkers(file);
        validateYaml(file, (JsonDocument) document);
        validateSwagger(file, (JsonDocument) document, fileEditorInput);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IFileEditorInput(org.eclipse.ui.IFileEditorInput) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 64 with IDocument

use of org.eclipse.jface.text.IDocument in project KaiZen-OpenAPI-Editor by RepreZen.

the class TextDocumentMarkerResolution method run.

public void run(IMarker marker) {
    try {
        IResource resource = marker.getResource();
        if (resource.getType() != IResource.FILE) {
            throw new CoreException(createStatus(null, "The editor is not a File: " + resource.getName()));
        }
        IFile file = (IFile) resource;
        ITextEditor editor = openTextEditor(file);
        IDocument document = editor.getDocumentProvider().getDocument(new FileEditorInput(file));
        if (document == null) {
            throw new CoreException(createStatus(null, "The document is null"));
        }
        IRegion region = processFix(document, marker);
        if (region != null) {
            editor.selectAndReveal(region.getOffset(), region.getLength());
        }
    } catch (CoreException e) {
        Activator.getDefault().getLog().log(e.getStatus());
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) CoreException(org.eclipse.core.runtime.CoreException) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion)

Example 65 with IDocument

use of org.eclipse.jface.text.IDocument in project cubrid-manager by CUBRID.

the class SQLContentAssistProcessor method computeCompletionProposals.

public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    IDocument document = viewer.getDocument();
    int currOffset = offset - 1;
    try {
        String currWord = "";
        char currChar;
        while (currOffset >= 0 && !isKeywordSeparator(currChar = document.getChar(currOffset))) {
            currWord = currChar + currWord;
            currOffset--;
        }
        currWord = trimQualifier(currWord);
        if (currWord.trim().length() == 0 && isSQLEndPos(document, currOffset)) {
            return null;
        }
        // if currWord is "" or AssistProcessor is displaying return null;
        if (currWord.trim().equals("") && isRunning()) {
            return null;
        }
        setRunning(true);
        List<ICompletionProposal> finalProposals = new ArrayList<ICompletionProposal>();
        if (GeneralPreference.isAutoCompleteTablesOrColumns()) {
            // columns
            List<ColumnProposalDetailInfo> tableColumns = getColumnNames(currWord.toUpperCase(Locale.getDefault()), document, offset - 1);
            List<ICompletionProposal> columnProposals = buildColumnProposals(tableColumns, currWord, offset - currWord.length(), CommonUIPlugin.getImage("icons/navigator/table_column_item.png"), false);
            finalProposals.addAll(columnProposals);
            // tables
            List<String> tableNames = getTableNames(currWord.toUpperCase(Locale.getDefault()), document, offset - 1);
            List<ICompletionProposal> tableProposals = buildProposals(tableNames, currWord, offset - currWord.length(), CommonUIPlugin.getImage("icons/navigator/schema_table_item.png"), false);
            finalProposals.addAll(tableProposals);
        }
        // keywords
        if (GeneralPreference.isAutoCompleteKeyword()) {
            List<String> suggestions = wordTracker.suggest(currWord.toUpperCase(Locale.getDefault()), databaseProvider.getDatabaseInfo());
            List<ICompletionProposal> keywordProposals = buildProposals(suggestions, currWord, offset - currWord.length(), CommonUIPlugin.getImage("icons/navigator/sql.png"), true);
            finalProposals.addAll(keywordProposals);
        }
        return finalProposals.toArray(new ICompletionProposal[finalProposals.size()]);
    } catch (BadLocationException e) {
        LOGGER.error("", e);
        lastError = e.getMessage();
        return null;
    } finally {
        setRunning(false);
    }
}
Also used : ICompletionProposal(com.cubrid.common.ui.query.control.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IDocument (org.eclipse.jface.text.IDocument)488 BadLocationException (org.eclipse.jface.text.BadLocationException)195 Document (org.eclipse.jface.text.Document)118 Test (org.junit.Test)93 IRegion (org.eclipse.jface.text.IRegion)72 Point (org.eclipse.swt.graphics.Point)63 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)54 Position (org.eclipse.jface.text.Position)51 ArrayList (java.util.ArrayList)44 CoreException (org.eclipse.core.runtime.CoreException)39 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)30 IPath (org.eclipse.core.runtime.IPath)26 IFile (org.eclipse.core.resources.IFile)25 IStatus (org.eclipse.core.runtime.IStatus)25 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)24 TextEdit (org.eclipse.text.edits.TextEdit)23 Region (org.eclipse.jface.text.Region)22 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)22 LinkedPositionGroup (org.eclipse.jface.text.link.LinkedPositionGroup)21 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)20