Datagrip editor get active text

已计划

In DataGrip when writing a query in query console I can see a highlight around the query I am working on

I want the plugin I am developing to get this text
not all the text from the editor 
just the query that is currently highlighted

I tried this but I alway get NULL
editor.selectionModel.selectedText

This works but gives all text in editor
editor.document.text

I am using InteliJ IDEA, Kotlin, Gradle
 

0
正式评论

Hello Ewenson!

You can process all relevant statements as follows:


public static void handleConsoleQuery(@NotNull final Project project, @NotNull final PsiFile file, @NotNull final Editor editor) {
  JdbcConsole console = JdbcConsoleProvider.getValidConsole(project, file.getVirtualFile());
  if (console == null) return;
  PsiElement elementAt = JdbcConsoleProvider.elementAt(file, null, editor);
  if (elementAt == null) return;
  DatabaseSettings.ExecOption execOption = DatabaseSettings.getDefaultExecOption();
  JdbcConsoleProvider.Info info = JdbcConsoleProvider.findScriptModel(file, elementAt, editor, execOption);
  if (info == null) return;
  JdbcConsoleProvider.
    chooseStatements(info, null, false, model -> {
      /*
       * Iterate through all statements in the selection.
       * In case there is a caret but no selection, there will be exactly one statement guaranteed.
       */
      for (ScriptModel.StatementIt<?> it : model.statements()) {
        try {
          String query = it.consoleQuery(console.getPStorage(), Conditions.alwaysFalse());
          handleConsoleQuery(query);
        } catch (TranslateException ignored) {
        }
      }
    });
}

private static void handleConsoleQuery(@NotNull String query) {
  // ...
}

The same logic is written here as used to determine the position of the frame. 

(!) Please note that there may be more than one statement within the frame. (!)

请先登录再写评论。