External_tool

What is IntelliJ external tool?

It's a way to link an external command (eg. apple script or shell script) to run from IntelliJ. Allowing to assign a shortcut within IntelliJ for activation.

Highlighted notes

How to integrate bash function as an external tool?

What is IntelliJ external tool?

It's a way to link an external command (eg. apple script or shell script) to run from IntelliJ. Allowing to assign a shortcut within IntelliJ for activation.

Next question: Why would You want to integrate a something like a bash function as an external tool?

Creating IntelliJ plugin is a much higher undertaking then being able to link up to some quick bash functionality.

Why go through apple script to use bash?

Use apple script to activate an existing terminal for the following reasons:

  • Allows usage of interactive commands such as fzf
  • Allows using preloaded bash environment avoiding having to source bash files and making the external tools work snappy.

How to do it:

Setup apple script

Create an apple script:

Example:

/Users/nkondrat/vintrin-env/IntelliJ/external_tools/fzf_file.applescript

With content like:

on run argv
  if (count of argv) = 0 then
    display alert "No file path provided!"
    return
  end if

  set filePath to item 1 of argv

  tell application "Terminal"
    activate
    do script "intellij_external_tools.fzf_for_file " & quoted form of filePath in front window
  end tell
end run

Note in above example my main Terminal driver is iTerm but I use Terminal for the external tool so that it can be positioned in a way to aligns with how I want it to appear for usage of external tool.

Setup bash function

intellij_external_tools.fzf_for_file is a bash function in my environment that is ready to accept a file.

Bash function
intellij_external_tools.fzf_for_file(){
  echo.func "${@}"

  intellij.for_given_file.open_at_line_using_fzf "${@}"
}
intellij.for_given_file.open_at_line_using_fzf ()
{
    echo.func "${@}";
    local file="${1:?file}";
    echo "${file:?}" | FileLineReferenceTabbed.create.from_piped_in.file_paths | FileLineReferenceTabbed.fzf | FileLineReferenceTabbed.open_in_intellij
}

Setup External Tool in IntelliJ

In Settings -> Tools -> External Tools add a new tool such as:

img

Notice how the program is osascript and the first argument is the script that we want to run. THe $FilePath$ is intellij provided argument (you can find a list of available arguments by clicking the + icon).

Note Worthy examples

Use existing terminal session without switching focus to it

Here just comment out (remove) 'activate' line in the apple script. And you will be able to perform actions on terminal session that already exists (all the sourcing has been done). Without loosing focus from IntelliJ.

on run argv
  if (count of argv) = 0 then
    display alert "No file path provided!"
    return
  end if

  set filePath to item 1 of argv

  tell application "Terminal"
--    activate

    do script "intellij_external_tools.copy_file_content_with_file_path " & quoted form of filePath in front window
  end tell
end run

Macros available for input

  1. SelectionStartColumn - Selected text start column number
  2. SelectionStartLine - Selected text start line number
  3. Sourcepath - Project source path
  4. SourcepathEntry - Entry in the source path the element belongs to
  5. TempDir - The directory for temporary files
  6. UnixSeparators - Takes a parameter and converts separators to ‘/’. Example: $UnixSeparators(\foo\bar) → /foo/bar/
  7. FileRelativeDir - File directory relative to the project file
  8. FileRelativePath - File path relative to the project file
  9. IsMake - Boolean value according to the performed compilation: true for make, false for force recompile
  10. JavaDocPath - JavaDoc output directory
  11. JDKPath - JDK path
  12. LineNumber - Line number
  13. ModuleFileDir - The directory of the module file
  14. ModuleFilePath - The path to the module file
  15. ModuleName - The name of the module file without extension
  16. ModuleSdkPath - Module SDK path
  17. ModuleSourcePath - Module source path
  18. OSName - The name of the operating system in lowercase
  19. OSUser - The name of the operating system user
  20. OutputPath - Output path
  21. Password - Displays a password input dialog
  22. ProjectFileDir - The directory of the project file
  23. ProjectName - The name of the project file without extension
  24. Projectpath - Project source path
  25. Prompt - Displays a string input dialog
  26. PyInterpreterDirectory - The directory containing the Python interpreter selected for the project
  27. RemoteProjectFileDir - Full path to the project directory on a remote server
  28. SelectedText - Text selected in the editor
  29. SelectionEndColumn - Selected text end column number
  30. SelectionEndLine - Selected text end line number
  31. AffectedModuleNames - The names of modules in the scope, comma-separated
  32. Classpath - Project’s classpath
  33. ClasspathEntry - Entry in the classpath the element belongs to
  34. ClipboardContent - The clipboard content
  35. ColumnNumber - Column number
  36. ContentRoot - Content root path the file belongs to
  37. FileClass - Class name
  38. FileDir - File directory
  39. FileDirName - File directory name
  40. FileDirPathFromParent - Path to FileDirFileDir from the parent directory with the name passed as a parameter
  41. FileDirRelativeToProjectRoot - File directory relative to the module content root the file belongs to
  42. FileDirRelativeToSourcepath - File directory relative to the source path root the file belongs to
  43. FileEncoding - File encoding
  44. FileExt - File extension
  45. FileFQPackage - File fully qualified package
  46. FileName - File name
  47. FileNameWithoutAllExtensions - File name without all extensions
  48. FileNameWithoutExtension - File name without extension
  49. FilePackage - File package
  50. FileParentDir - File parent directory. Takes an optional parameter (name) to find the parent directory
  51. FilePath - File path
  52. FilePathRelativeToProjectRoot - File path relative to the module content root the file belongs to
  53. FilePathRelativeToSourcepath - File path relative to the source path root the file belongs to
  54. FilePrompt - Shows a file chooser dialog


Children
  1. How to: Integrate Bash Function as External Tool
  2. Macros Available for Input
  3. Notes