Search in sources :

Example 26 with Document

use of org.eclipse.jface.text.Document in project translationstudio8 by heartsome.

the class TagStyleConfigurator method configure.

public static void configure(TextLayout textLayout) {
    String text = textLayout.getText();
    Document doc = new Document(text);
    ITokenScanner scanner = getRecipeScanner(doc);
    scanner.setRange(doc, 0, doc.getLength());
    IToken token;
    while ((token = scanner.nextToken()) != Token.EOF) {
        int offset = scanner.getTokenOffset();
        int length = scanner.getTokenLength();
        Object data = token.getData();
        if (data != null && data instanceof TextStyle) {
            TextStyle textStyle = (TextStyle) data;
            textLayout.setStyle(textStyle, offset, offset + length - 1);
        }
    }
    scanner = null;
    doc = null;
}
Also used : IToken(org.eclipse.jface.text.rules.IToken) TextStyle(org.eclipse.swt.graphics.TextStyle) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ITokenScanner(org.eclipse.jface.text.rules.ITokenScanner)

Example 27 with Document

use of org.eclipse.jface.text.Document in project AutoRefactor by JnRouvignac.

the class TestHelper method normalizeJavaSourceCode.

public static String normalizeJavaSourceCode(String source) {
    final CodeFormatter codeFormatter = createCodeFormatter(getJava7Options());
    final TextEdit edit = codeFormatter.format(K_COMPILATION_UNIT, // source to format
    source, // source to format
    0, // source to format
    source.length(), // initial indentation and line separator
    0, // initial indentation and line separator
    System.getProperty("line.separator"));
    try {
        final IDocument document = new Document(source);
        edit.apply(document);
        return document.get();
    } catch (MalformedTreeException e) {
        throw new RuntimeException(e);
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
}
Also used : CodeFormatter(org.eclipse.jdt.core.formatter.CodeFormatter) TextEdit(org.eclipse.text.edits.TextEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 28 with Document

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

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

the class CodeView method createPartControl.

@Override
public void createPartControl(Composite parent) {
    this.parent = parent;
    parent.setLayout(new FillLayout());
    ECodeLanguage language = LanguageManager.getCurrentLanguage();
    ISourceViewer viewer = null;
    final StyledText text;
    int styles = SWT.H_SCROLL | SWT.V_SCROLL;
    document = new Document();
    switch(language) {
        case JAVA:
            IPreferenceStore store = JavaPlugin.getDefault().getCombinedPreferenceStore();
            viewer = new JavaSourceViewer(parent, null, null, false, styles, store);
            viewer.setDocument(document);
            JavaTextTools tools = JavaPlugin.getDefault().getJavaTextTools();
            tools.setupJavaDocumentPartitioner(viewer.getDocument(), IJavaPartitions.JAVA_PARTITIONING);
            SimpleJavaSourceViewerConfiguration config = new SimpleJavaSourceViewerConfiguration(tools.getColorManager(), store, null, IJavaPartitions.JAVA_PARTITIONING, true);
            viewer.configure(config);
            viewer.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
            document = viewer.getDocument();
            break;
        // empty since only have java
        default:
    }
    viewer.setEditable(false);
    text = viewer.getTextWidget();
    // getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this);
    IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
    IAction wrapAction = new Action() {

        @Override
        public void run() {
            text.setWordWrap(isChecked());
        }
    };
    //$NON-NLS-1$
    wrapAction.setToolTipText("wrap");
    wrapAction.setChecked(false);
    //$NON-NLS-1$
    wrapAction.setImageDescriptor(ImageDescriptor.createFromFile(DesignerPlugin.class, "/icons/wrap.gif"));
    tbm.add(wrapAction);
    createMenu();
}
Also used : IAction(org.eclipse.jface.action.IAction) Action(org.eclipse.jface.action.Action) StyledText(org.eclipse.swt.custom.StyledText) SimpleJavaSourceViewerConfiguration(org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration) IAction(org.eclipse.jface.action.IAction) JavaTextTools(org.eclipse.jdt.ui.text.JavaTextTools) FillLayout(org.eclipse.swt.layout.FillLayout) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DesignerPlugin(org.talend.designer.core.DesignerPlugin) JavaSourceViewer(org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer) IToolBarManager(org.eclipse.jface.action.IToolBarManager) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) ECodeLanguage(org.talend.core.language.ECodeLanguage)

Example 30 with Document

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

the class TalendJavaSourceViewer method createViewerForIfConnection.

public static ISourceViewer createViewerForIfConnection(Composite composite) {
    StringBuffer buff = new StringBuffer();
    //$NON-NLS-1$
    buff.append("package internal;\n\n");
    buff.append(getImports());
    //$NON-NLS-1$ //$NON-NLS-2$
    buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
    //$NON-NLS-1$
    buff.append("\tprivate static java.util.Properties context = new java.util.Properties();\n");
    //$NON-NLS-1$
    buff.append("\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n");
    //$NON-NLS-1$
    buff.append("\tpublic void myFunction(){\n");
    //$NON-NLS-1$
    buff.append("\t  if( \n");
    int documentOffset = buff.toString().length();
    //$NON-NLS-1$
    buff.append("){\n\t}");
    //$NON-NLS-1$
    buff.append("\n\t\n}\n}");
    IDocument document = new Document();
    document.set(buff.toString());
    boolean checkCode = false;
    int styles = SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.WRAP;
    return initializeViewer(composite, styles, checkCode, document, documentOffset);
}
Also used : Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument)

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