Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ca0f07f
Allows auto-completion even with no connection and added some basic h…
ebresie Mar 20, 2021
e6d4070
Allows auto-completion even with no connection and added some basic h…
ebresie Mar 20, 2021
96b4f70
Updates based on PR comments. Removed "annotation" and related items…
ebresie Apr 3, 2021
8935aba
Removed reference to netbeans-modules-db-sql-editor_annotation.xml wh…
ebresie Apr 3, 2021
7755bda
Removed reference to spi.java.hints from project.xml file which is no…
ebresie Apr 5, 2021
9fb0a8c
Removed unused LAB_db_sql_editor_annotation which is not needed
ebresie Apr 9, 2021
b2ba9a8
Removed fix/hints and related updates.
ebresie Apr 9, 2021
dce7e04
Added notification of no connection when applicable.
ebresie Apr 9, 2021
5fb5027
Changes getUnquotedIdentifier to return current text without checking…
ebresie Apr 9, 2021
7809a7a
Removed notification warning.
ebresie Apr 9, 2021
eae8176
Removed unused imports.
ebresie Apr 9, 2021
84b3508
[NETBEANS-189] Allows auto-completion even with no connection
ebresie Mar 20, 2021
c82fd4a
[NETBEANS-189] Allows auto-completion even with no connection
ebresie Apr 11, 2021
990b606
Merge branch 'sql_autocomplete' of https://github.com/ebresie/netbean…
ebresie Apr 11, 2021
56ed922
Merge branch 'sql_autocomplete' of https://github.com/ebresie/netbean…
ebresie Apr 11, 2021
8ee7b3e
Merge branch 'sql_autocomplete' of https://github.com/ebresie/netbean…
ebresie Apr 15, 2021
ad9eb22
Merge branch 'sql_autocomplete' of https://github.com/ebresie/netbean…
ebresie Apr 15, 2021
6f7735c
Merge branch 'sql_autocomplete' of https://github.com/ebresie/netbean…
ebresie Apr 15, 2021
912ac4f
Merge branch 'sql_autocomplete' of https://github.com/ebresie/netbean…
ebresie Apr 15, 2021
e2f928d
Merge branch 'sql_autocomplete' of https://github.com/ebresie/netbean…
ebresie Apr 15, 2021
11150b7
Allows auto-completion even with no connection.
ebresie Mar 20, 2021
a036c0e
Updates based on PR comments. Removed "annotation" and related items…
ebresie Apr 3, 2021
33a59c8
Update project.xml
ebresie Jul 16, 2021
4be31d4
Update SQLCompletionProvider.java
ebresie Jul 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.netbeans.modules.db.sql.analyzer.SQLStatement.Context;
import org.netbeans.modules.db.sql.editor.StringUtils;
import org.netbeans.modules.db.sql.lexer.SQLTokenId;

/**
*
* @author Jiri Rechtacek, Jiri Skrivanek
Expand Down Expand Up @@ -239,6 +238,10 @@ protected QualIdent parseIdentifier() {
}

protected String getUnquotedIdentifier() {
// quoter unavailable so returnas is
if (quoter == null) {
return seq.token().text().toString();
}
return quoter.unquote(seq.token().text().toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.api.db.explorer.DatabaseConnection;
import org.netbeans.api.editor.completion.Completion;
import org.netbeans.api.lexer.Language;
import org.netbeans.api.lexer.TokenHierarchy;
import org.netbeans.api.lexer.TokenSequence;
Expand All @@ -41,39 +40,61 @@
* @author Andrei Badea
*/
public class SQLCompletionProvider implements CompletionProvider {

@Override
public CompletionTask createTask(int queryType, JTextComponent component) {
if (queryType == CompletionProvider.COMPLETION_QUERY_TYPE || queryType == CompletionProvider.COMPLETION_ALL_QUERY_TYPE) {
if (queryType == CompletionProvider.COMPLETION_QUERY_TYPE ||
queryType == CompletionProvider.COMPLETION_ALL_QUERY_TYPE) {

/* to support DB related completion tasks (i.e. auto populating table
names or db columns for given schema) check for connection */
DatabaseConnection dbconn = findDBConn(component);
if (dbconn == null) {
// XXX perhaps should have an item in the completion instead?
Completion.get().hideAll();
SQLExecutionBaseAction.notifyNoDatabaseConnection();
return null;
}

return new AsyncCompletionTask(new SQLCompletionQuery(dbconn), component);
}

// not a completion query type so return nothing
return null;
}

/**
* getAutoQueryTypes is invoked to check whether a popup with suggestions
* should be shown without the user explicitly asking for it.
*
* If either #getAutoQueryTypes return a non-zero value or the user
* explicitly asks for completion, #createTask is invoked with the
* requested type. In case of SQL see
* org.netbeans.modules.db.sql.editor.completion.SQLCompletionQuery.
*
* @param component
* @param typedText
* @return
*/
public int getAutoQueryTypes(JTextComponent component, String typedText) {
// XXX: Check if "enable/disable" autocomplete is setting. See NETBEANS-188
// If "." has not been typed then acceptable to start checking for options.
if (!".".equals(typedText)) { // NOI18N
return 0;
}
// check typed text if dot is present at the selected offset
if (!isDotAtOffset(component, component.getSelectionStart() - 1)) {
return 0;
}

// check if there is a DB connection
DatabaseConnection dbconn = findDBConn(component);
if (dbconn == null) {
String message = NbBundle.getMessage(SQLCompletionProvider.class, "MSG_NoDatabaseConnection");
StatusDisplayer.getDefault().setStatusText(message);
return 0;
SQLExecutionBaseAction.notifyNoDatabaseConnection();
}
if (dbconn.getJDBCConnection() == null) {

// check if DB connection is active
if (dbconn != null && dbconn.getJDBCConnection() == null) {
String message = NbBundle.getMessage(SQLCompletionProvider.class, "MSG_NotConnected");
StatusDisplayer.getDefault().setStatusText(message);
return 0;
}
SQLExecutionBaseAction.notifyNoDatabaseConnection();
// XXX: Maybe add content specific "fixs"
}
Comment thread
ebresie marked this conversation as resolved.
return COMPLETION_QUERY_TYPE;
}

Expand Down Expand Up @@ -101,24 +122,35 @@ private static Lookup findContext(JTextComponent component) {
return null;
}

/**
* For given component object's doc model, determine if given offset
* has applicable dot delimiter token
* @param component
* @param offset
* @return
*/
private static boolean isDotAtOffset(JTextComponent component, final int offset) {
final Document doc = component.getDocument();
final boolean[] result = { false };
doc.render(new Runnable() {
public void run() {
TokenSequence<SQLTokenId> seq = getSQLTokenSequence(doc);
if (seq == null) {
return;
}
seq.move(offset);
if (!seq.moveNext() && !seq.movePrevious()) {
return;
}
if (seq.offset() != offset) {
return;
}
result[0] = (seq.token().id() == SQLTokenId.DOT);
// trigger internal model rendering based on SQL tokens
doc.render(() -> {
TokenSequence<SQLTokenId> seq = getSQLTokenSequence(doc);
// no SQL tokens found; done checking
if (seq == null) {
return;
}
// in the sequence move to provided offset location
seq.move(offset);
// no next or previous so done looking for tokens; done checking
if (!seq.moveNext() && !seq.movePrevious()) {
return;
}
// if offset not consistent with sequence offset done checking
if (seq.offset() != offset) {
return;
}
// confirm token ID is applicable SQL "dot" token
result[0] = (seq.token().id() == SQLTokenId.DOT);
});
return result[0];
}
Expand Down
Loading