SQLCompletionProvider extends CompletionProviderBase { public List<Completion> getCompletionsAtIndex(JTextComponent jTextComponent, int charIndex) { UpdateParserThread fetchingParserTmp = fetchingParser; if(fetchingParserTmp != null) { if(!fetchingParserTmp.isDone()) { try { fetchingParserTmp.get(UPDATE_TIMEOUT, TimeUnit.MILLISECONDS); } catch (Exception ex) { return new ArrayList<>(); } } else { parser = fetchingParserTmp.getParser(); } } if(parser == null) { return new ArrayList<>(); } List<Completion> completionList = new LinkedList<Completion>(); DocumentSQLReader documentReader = new DocumentSQLReader(jTextComponent.getDocument(), true); String statement = ""; while(documentReader.hasNext() && documentReader.getPosition() + statement.length() < charIndex) { statement = documentReader.next(); } int completionPosition = charIndex - documentReader.getPosition(); String partialStatement = LTRIM.matcher(statement.substring(0, completionPosition)).replaceAll(""); Map<String,String> autoComplete = parser.getNextTokenList(partialStatement); for(Map.Entry<String, String> entry : autoComplete.entrySet()) { String token = entry.getKey().substring(entry.getKey().indexOf("#") + 1); Completion completion = new BnfAutoCompletion(this, token, entry.getValue()); completionList.add(completion); } long now = System.currentTimeMillis(); if(lastUpdate + UPDATE_INTERVAL < now) { try { updateParser(dataSource, false); } catch (Exception ex) { log.warn("Could not update auto-completion engine", ex); } } return completionList; } SQLCompletionProvider(DataSource dataSource, boolean immediateInit); void setDataSource(DataSource ds); void updateParser(DataSource dataSource, boolean immediateInit); @Override String getAlreadyEnteredText(JTextComponent jTextComponent); List<Completion> getCompletionsAtIndex(JTextComponent jTextComponent, int charIndex); @Override List getCompletionsAt(JTextComponent jTextComponent, Point point); @Override List getParameterizedCompletions(JTextComponent jTextComponent);  }
@Test public void testBounds() throws Exception { assumeTrue(!GraphicsEnvironment.isHeadless()); org.h2.Driver driver = org.h2.Driver.load(); OsgiDataSourceFactory dataSourceFactory = new OsgiDataSourceFactory(driver); Properties properties = new Properties(); properties.setProperty(OsgiDataSourceFactory.JDBC_URL, RSyntaxSQLParserTest.DATABASE_PATH); RSyntaxDocument document = new RSyntaxDocument("sql"); RSyntaxTextArea rSyntaxTextArea = new RSyntaxTextArea(document); rSyntaxTextArea.setText("alte"); rSyntaxTextArea.setSize(new Dimension(420, 240)); rSyntaxTextArea.setCaretPosition(4); SQLCompletionProvider autoComplete = new SQLCompletionProvider(dataSourceFactory.createDataSource(properties), true); List completions = autoComplete.getCompletionsAtIndex(rSyntaxTextArea, 4); assertEquals(1, completions.size()); assertEquals("ALTER", ((Completion)completions.get(0)).getReplacementText()); }
@Test public void testBoundsMultiLine() throws Exception { assumeTrue(!GraphicsEnvironment.isHeadless()); org.h2.Driver driver = org.h2.Driver.load(); OsgiDataSourceFactory dataSourceFactory = new OsgiDataSourceFactory(driver); Properties properties = new Properties(); properties.setProperty(OsgiDataSourceFactory.JDBC_URL, RSyntaxSQLParserTest.DATABASE_PATH); RSyntaxDocument document = new RSyntaxDocument("sql"); RSyntaxTextArea rSyntaxTextArea = new RSyntaxTextArea(document); rSyntaxTextArea.setText("CREATE TABLE toto as select 1;\n" + "\n" + "create ta"); rSyntaxTextArea.setSize(new Dimension(420, 240)); rSyntaxTextArea.setCaretPosition(41); SQLCompletionProvider autoComplete = new SQLCompletionProvider(dataSourceFactory.createDataSource(properties), true); List completions = autoComplete.getCompletionsAtIndex(rSyntaxTextArea, 41); assertEquals(1, completions.size()); assertEquals("TABLE", ((Completion)completions.get(0)).getReplacementText()); }