Search in sources :

Example 21 with Document

use of org.eclipse.jface.text.Document in project generator by mybatis.

the class ExistingJavaFileVisitorTest method testRegularInterface.

@Test
public void testRegularInterface() throws Exception {
    InputStream resource = getClass().getResourceAsStream("/org/mybatis/generator/eclipse/core/tests/merge/resources/AwfulTableMapper.java.src");
    String source = getResourceAsString(resource);
    IDocument document = new Document(source);
    CompilationUnit cu = getCompilationUnitFromSource(source);
    cu.recordModifications();
    ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(MergeConstants.OLD_ELEMENT_TAGS);
    // delete all the old generated stuff
    cu.accept(visitor);
    assertThat(visitor.getTypeDeclaration(), is(notNullValue()));
    assertThat(visitor.getTypeDeclaration().isInterface(), is(true));
    assertThat(visitor.getTypeDeclaration().getName().getFullyQualifiedName(), is("AwfulTableMapper"));
    // generate a new compilation unit that is stripped of old stuff
    TextEdit textEdit = cu.rewrite(document, null);
    textEdit.apply(document);
    CompilationUnitSummary summary = getCompilationUnitSummaryFromSource(document.get());
    assertThat(summary, hasImportCount(11));
    assertThat(summary, hasInterface("AwfulTableMapper"));
    assertThat(summary, hasInterface("AwfulTableMapper", withSuperInterfaceCount(0)));
    assertThat(summary, hasInterface("AwfulTableMapper", withMethodCount(0)));
    assertThat(summary, hasInterface("AwfulTableMapper", withFieldCount(0)));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) InputStream(java.io.InputStream) TextEdit(org.eclipse.text.edits.TextEdit) CompilationUnitSummary(org.mybatis.generator.eclipse.tests.harness.summary.CompilationUnitSummary) Utilities.getResourceAsString(org.mybatis.generator.eclipse.tests.harness.Utilities.getResourceAsString) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ExistingJavaFileVisitor(org.mybatis.generator.eclipse.core.merge.ExistingJavaFileVisitor) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 22 with Document

use of org.eclipse.jface.text.Document in project dbeaver by serge-rider.

the class SQLEditorPropertyTester method test.

@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    if (!(receiver instanceof SQLEditorBase)) {
        return false;
    }
    SQLEditor editor = (SQLEditor) receiver;
    final Control editorControl = editor.getEditorControl();
    if (editorControl == null) {
        return false;
    }
    boolean hasConnection = editor.getDataSourceContainer() != null;
    switch(property) {
        case PROP_CAN_EXECUTE:
            // Do not check hasActiveQuery - sometimes jface don't update action enablement after cursor change/typing
            return hasConnection;
        /* && (!"statement".equals(expectedValue) || editor.hasActiveQuery())*/
        case PROP_CAN_EXPLAIN:
            return hasConnection && DBUtils.getAdapter(DBCQueryPlanner.class, editor.getDataSource()) != null;
        case PROP_CAN_NAVIGATE:
            {
                // Check whether some word is under cursor
                ISelectionProvider selectionProvider = editor.getSelectionProvider();
                if (selectionProvider == null) {
                    return false;
                }
                ITextSelection selection = (ITextSelection) selectionProvider.getSelection();
                Document document = editor.getDocument();
                return selection != null && document != null && !new SQLIdentifierDetector(editor.getSyntaxManager().getStructSeparator(), editor.getSyntaxManager().getQuoteSymbol()).detectIdentifier(document, new Region(selection.getOffset(), selection.getLength())).isEmpty();
            }
        case PROP_CAN_EXPORT:
            return hasConnection && editor.hasActiveQuery();
        case PROP_HAS_SELECTION:
            {
                ISelection selection = editor.getSelectionProvider().getSelection();
                return selection instanceof ITextSelection && ((ITextSelection) selection).getLength() > 0;
            }
    }
    return false;
}
Also used : Control(org.eclipse.swt.widgets.Control) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ISelection(org.eclipse.jface.viewers.ISelection) SQLIdentifierDetector(org.jkiss.dbeaver.ui.editors.sql.syntax.SQLIdentifierDetector) Region(org.eclipse.jface.text.Region) Document(org.eclipse.jface.text.Document) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 23 with Document

use of org.eclipse.jface.text.Document in project dbeaver by serge-rider.

the class ParseUtils method main.

public static void main(String[] args) {
    System.out.println("Test SCM parser");
    String sql = "SELECT * FROM SCHEMA.TABLE WHERE COL1 <> 100 AND COL2 = 'TEST'";
    SCMRoot nodeTree = ParseUtils.parseDocument(new Document(sql), new SQLNodeParser());
    System.out.println(nodeTree);
}
Also used : SQLNodeParser(org.jkiss.dbeaver.lang.sql.SQLNodeParser) Document(org.eclipse.jface.text.Document)

Example 24 with Document

use of org.eclipse.jface.text.Document in project dbeaver by serge-rider.

the class ResultSetFilterPanel method getProposals.

@Override
public IContentProposal[] getProposals(String contents, int position) {
    SQLSyntaxManager syntaxManager = new SQLSyntaxManager();
    if (viewer.getDataContainer() != null) {
        syntaxManager.init(viewer.getDataContainer().getDataSource());
    }
    SQLWordPartDetector wordDetector = new SQLWordPartDetector(new Document(contents), syntaxManager, position);
    final String word = wordDetector.getFullWord().toLowerCase(Locale.ENGLISH);
    List<IContentProposal> proposals = new ArrayList<>();
    for (DBDAttributeBinding attribute : viewer.getModel().getAttributes()) {
        final String name = attribute.getName();
        if (CommonUtils.isEmpty(word) || name.toLowerCase(Locale.ENGLISH).startsWith(word)) {
            final String content = name.substring(word.length()) + " ";
            proposals.add(new ContentProposal(content, attribute.getName(), SQLContextInformer.makeObjectDescription(null, attribute.getAttribute(), false), content.length()));
        }
    }
    return proposals.toArray(new IContentProposal[proposals.size()]);
}
Also used : ContentProposal(org.eclipse.jface.fieldassist.ContentProposal) IContentProposal(org.eclipse.jface.fieldassist.IContentProposal) IContentProposal(org.eclipse.jface.fieldassist.IContentProposal) SQLWordPartDetector(org.jkiss.dbeaver.ui.editors.sql.syntax.SQLWordPartDetector) SQLSyntaxManager(org.jkiss.dbeaver.model.sql.SQLSyntaxManager) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 25 with Document

use of org.eclipse.jface.text.Document in project dbeaver by serge-rider.

the class BaseTextEditor method loadFromExternalFile.

public void loadFromExternalFile() {
    final File loadFile = DialogUtils.openFile(getSite().getShell(), new String[] { "*.sql", "*.txt", "*", "*.*" });
    if (loadFile == null) {
        return;
    }
    String newContent = null;
    try {
        try (Reader reader = new InputStreamReader(new FileInputStream(loadFile), GeneralUtils.DEFAULT_FILE_CHARSET)) {
            StringWriter buffer = new StringWriter();
            IOUtils.copyText(reader, buffer);
            newContent = buffer.toString();
        }
    } catch (IOException e) {
        UIUtils.showErrorDialog(getSite().getShell(), "Can't load file", "Can't load file '" + loadFile.getAbsolutePath() + "' - " + e.getMessage());
    }
    if (newContent != null) {
        Document document = getDocument();
        if (document != null) {
            document.set(newContent);
        }
    }
}
Also used : Document(org.eclipse.jface.text.Document) IFile(org.eclipse.core.resources.IFile)

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