Search in sources :

Example 1 with SQLCompletionAnalyzer

use of org.jkiss.dbeaver.model.sql.completion.SQLCompletionAnalyzer in project dbeaver by serge-rider.

the class SQLCompletionProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
    IDocument document = editor.getDocument();
    if (document == null) {
        return new ICompletionProposal[0];
    }
    final SQLCompletionRequest request = new SQLCompletionRequest(editor.getCompletionContext(), document, documentOffset, editor.extractQueryAtPos(documentOffset), simpleMode);
    SQLWordPartDetector wordDetector = request.getWordDetector();
    try {
        // Check that word start position is in default partition (#5994)
        String contentType = TextUtilities.getContentType(document, SQLParserPartitions.SQL_PARTITIONING, wordDetector.getStartOffset(), true);
        if (contentType == null || (!IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) && !SQLParserPartitions.CONTENT_TYPE_SQL_QUOTED.equals(contentType))) {
            return new ICompletionProposal[0];
        }
    } catch (BadLocationException e) {
        log.debug(e);
        return new ICompletionProposal[0];
    }
    if (lookupTemplates) {
        return makeTemplateProposals(viewer, request);
    }
    try {
        String commandPrefix = editor.getSyntaxManager().getControlCommandPrefix();
        if (wordDetector.getStartOffset() >= commandPrefix.length() && viewer.getDocument().get(wordDetector.getStartOffset() - commandPrefix.length(), commandPrefix.length()).equals(commandPrefix)) {
            return makeCommandProposals(request, request.getWordPart());
        }
    } catch (BadLocationException e) {
        log.debug(e);
    }
    SQLCompletionAnalyzer analyzer = new SQLCompletionAnalyzer(request);
    DBPDataSource dataSource = editor.getDataSource();
    if (request.getWordPart() != null) {
        if (dataSource != null) {
            ProposalSearchJob searchJob = new ProposalSearchJob(analyzer);
            searchJob.schedule();
            // Wait until job finished
            UIUtils.waitJobCompletion(searchJob);
        }
    }
    List<SQLCompletionProposalBase> proposals = analyzer.getProposals();
    List<ICompletionProposal> result = new ArrayList<>();
    for (SQLCompletionProposalBase cp : proposals) {
        if (cp instanceof ICompletionProposal) {
            result.add((ICompletionProposal) cp);
        }
    }
    return ArrayUtils.toArray(ICompletionProposal.class, result);
}
Also used : SQLWordPartDetector(org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector) SQLCompletionProposalBase(org.jkiss.dbeaver.model.sql.completion.SQLCompletionProposalBase) ArrayList(java.util.ArrayList) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLCompletionRequest(org.jkiss.dbeaver.model.sql.completion.SQLCompletionRequest) SQLCompletionAnalyzer(org.jkiss.dbeaver.model.sql.completion.SQLCompletionAnalyzer)

Example 2 with SQLCompletionAnalyzer

use of org.jkiss.dbeaver.model.sql.completion.SQLCompletionAnalyzer in project dbeaver by dbeaver.

the class SQLCompletionProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
    IDocument document = editor.getDocument();
    if (document == null) {
        return new ICompletionProposal[0];
    }
    final SQLCompletionRequest request = new SQLCompletionRequest(editor.getCompletionContext(), document, documentOffset, editor.extractQueryAtPos(documentOffset), simpleMode);
    SQLWordPartDetector wordDetector = request.getWordDetector();
    try {
        // Check that word start position is in default partition (#5994)
        String contentType = TextUtilities.getContentType(document, SQLParserPartitions.SQL_PARTITIONING, wordDetector.getStartOffset(), true);
        if (contentType == null || (!IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) && !SQLParserPartitions.CONTENT_TYPE_SQL_QUOTED.equals(contentType))) {
            return new ICompletionProposal[0];
        }
    } catch (BadLocationException e) {
        log.debug(e);
        return new ICompletionProposal[0];
    }
    if (lookupTemplates) {
        return makeTemplateProposals(viewer, request);
    }
    try {
        String commandPrefix = editor.getSyntaxManager().getControlCommandPrefix();
        if (wordDetector.getStartOffset() >= commandPrefix.length() && viewer.getDocument().get(wordDetector.getStartOffset() - commandPrefix.length(), commandPrefix.length()).equals(commandPrefix)) {
            return makeCommandProposals(request, request.getWordPart());
        }
    } catch (BadLocationException e) {
        log.debug(e);
    }
    SQLCompletionAnalyzer analyzer = new SQLCompletionAnalyzer(request);
    DBPDataSource dataSource = editor.getDataSource();
    if (request.getWordPart() != null) {
        if (dataSource != null) {
            ProposalSearchJob searchJob = new ProposalSearchJob(analyzer);
            searchJob.schedule();
            // Wait until job finished
            UIUtils.waitJobCompletion(searchJob);
        }
    }
    List<SQLCompletionProposalBase> proposals = analyzer.getProposals();
    List<ICompletionProposal> result = new ArrayList<>();
    for (SQLCompletionProposalBase cp : proposals) {
        if (cp instanceof ICompletionProposal) {
            result.add((ICompletionProposal) cp);
        }
    }
    return ArrayUtils.toArray(ICompletionProposal.class, result);
}
Also used : SQLWordPartDetector(org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector) SQLCompletionProposalBase(org.jkiss.dbeaver.model.sql.completion.SQLCompletionProposalBase) ArrayList(java.util.ArrayList) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLCompletionRequest(org.jkiss.dbeaver.model.sql.completion.SQLCompletionRequest) SQLCompletionAnalyzer(org.jkiss.dbeaver.model.sql.completion.SQLCompletionAnalyzer)

Aggregations

ArrayList (java.util.ArrayList)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 SQLCompletionAnalyzer (org.jkiss.dbeaver.model.sql.completion.SQLCompletionAnalyzer)2 SQLCompletionProposalBase (org.jkiss.dbeaver.model.sql.completion.SQLCompletionProposalBase)2 SQLCompletionRequest (org.jkiss.dbeaver.model.sql.completion.SQLCompletionRequest)2 SQLWordPartDetector (org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector)2