nimsuggest.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ================================
  2. Nim IDE Integration Guide
  3. ================================
  4. :Author: Unknown
  5. :Version: |nimversion|
  6. .. default-role:: code
  7. .. include:: rstcommon.rst
  8. .. contents::
  9. Nim differs from many other compilers in that it is really fast,
  10. and being so fast makes it suited to provide external queries for
  11. text editors about the source code being written. Through the
  12. `nimsuggest`:cmd: tool, any IDE
  13. can query a ``.nim`` source file and obtain useful information like
  14. definition of symbols or suggestions for completion.
  15. This document will guide you through the available options. If you
  16. want to look at practical examples of nimsuggest support you can look
  17. at the
  18. `various editor integrations <https://github.com/Araq/Nim/wiki/Editor-Support>`_
  19. already available.
  20. Installation
  21. ============
  22. Nimsuggest is part of Nim's core. Build it via:
  23. .. code:: cmd
  24. koch nimsuggest
  25. Nimsuggest invocation
  26. =====================
  27. Run it via `nimsuggest --stdin --debug myproject.nim`:cmd:. Nimsuggest is a
  28. server that takes queries that are related to `myproject`. There is some
  29. support so that you can throw random ``.nim`` files which are not part
  30. of `myproject` at Nimsuggest too, but usually the query refer to modules/files
  31. that are part of `myproject`.
  32. `--stdin`:option: means that Nimsuggest reads the query from `stdin`. This is great
  33. for testing things out and playing with it but for an editor communication
  34. via sockets is more reasonable so that is the default. It listens to port 6000
  35. by default.
  36. Nimsuggest is basically a frontend for the nim compiler so `--path`:option: flags and
  37. `config files <https://nim-lang.org/docs/nimc.html#compiler-usage-configuration-files>`_
  38. can be used to specify additional dependencies like
  39. `nimsuggest --stdin --debug --path:"dependencies" myproject.nim`:cmd:.
  40. Specifying the location of the query
  41. ------------------------------------
  42. Nimsuggest then waits for queries to process. A query consists of a
  43. cryptic 3 letter "command" `def` or `con` or `sug` or `use` followed by
  44. a location. A query location consists of:
  45. ``file.nim``
  46. This is the name of the module or include file the query refers to.
  47. ``dirtyfile.nim``
  48. This is optional.
  49. The `file` parameter is enough for static analysis, but IDEs
  50. tend to have *unsaved buffers* where the user may still be in
  51. the middle of typing a line. In such situations the IDE can
  52. save the current contents to a temporary file and then use the
  53. ``dirtyfile.nim`` option to tell Nimsuggest that ``foobar.nim`` should
  54. be taken from ``temporary/foobar.nim``.
  55. ``line``
  56. An integer with the line you are going to query. For the compiler
  57. lines start at **1**.
  58. ``col``
  59. An integer with the column you are going to query. For the
  60. compiler columns start at **0**.
  61. Definitions
  62. -----------
  63. The `def` Nimsuggest command performs a query about the definition
  64. of a specific symbol. If available, Nimsuggest will answer with the
  65. type, source file, line/column information and other accessory data
  66. if available like a docstring. With this information an IDE can
  67. provide the typical *Jump to definition* where a user puts the
  68. cursor on a symbol or uses the mouse to select it and is redirected
  69. to the place where the symbol is located.
  70. Since Nim is implemented in Nim, one of the nice things of
  71. this feature is that any user with an IDE supporting it can quickly
  72. jump around the standard library implementation and see exactly
  73. what a proc does, learning about the language and seeing real life
  74. examples of how to write/implement specific features.
  75. Nimsuggest will always answer with a single definition or none if it
  76. can't find any valid symbol matching the position of the query.
  77. Suggestions
  78. -----------
  79. The `sug` Nimsuggest command performs a query about possible
  80. completion symbols at some point in the file.
  81. The typical usage scenario for this option is to call it after the
  82. user has typed the dot character for `the object oriented call
  83. syntax <tut2.html#object-oriented-programming-method-call-syntax>`_.
  84. Nimsuggest will try to return the suggestions sorted first by scope
  85. (from innermost to outermost) and then by item name.
  86. Invocation context
  87. ------------------
  88. The `con` Nimsuggest command is very similar to the suggestions
  89. command, but instead of being used after the user has typed a dot
  90. character, this one is meant to be used after the user has typed
  91. an opening brace to start typing parameters.
  92. Symbol usages
  93. -------------
  94. The `use` Nimsuggest command lists all usages of the symbol at
  95. a position. IDEs can use this to find all the places in the file
  96. where the symbol is used and offer the user to rename it in all
  97. places at the same time.
  98. For this kind of query the IDE will most likely ignore all the
  99. type/signature info provided by Nimsuggest and concentrate on the
  100. filename, line and column position of the multiple returned answers.
  101. Parsing nimsuggest output
  102. =========================
  103. Nimsuggest output is always returned on single lines separated by
  104. tab characters (``\t``). The values of each column are:
  105. 1. Three characters indicating the type of returned answer (e.g.
  106. `def` for definition, `sug` for suggestion, etc).
  107. 2. Type of the symbol. This can be `skProc`, `skLet`, and just
  108. about any of the enums defined in the module ``compiler/ast.nim``.
  109. 3. Fully qualified path of the symbol. If you are querying a symbol
  110. defined in the ``proj.nim`` file, this would have the form
  111. `proj.symbolName`.
  112. 4. Type/signature. For variables and enums this will contain the
  113. type of the symbol, for procs, methods and templates this will
  114. contain the full unique signature (e.g. `proc (File)`).
  115. 5. Full path to the file containing the symbol.
  116. 6. Line where the symbol is located in the file. Lines start to
  117. count at **1**.
  118. 7. Column where the symbol is located in the file. Columns start
  119. to count at **0**.
  120. 8. Docstring for the symbol if available or the empty string. To
  121. differentiate the docstring from end of answer,
  122. the docstring is always provided enclosed in double quotes, and
  123. if the docstring spans multiple lines, all following lines of the
  124. docstring will start with a blank space to align visually with
  125. the starting quote.
  126. Also, you won't find raw ``\n`` characters breaking the one
  127. answer per line format. Instead you will need to parse sequences
  128. in the form ``\xHH``, where *HH* is a hexadecimal value (e.g.
  129. newlines generate the sequence ``\x0A``).