nimsuggest.rst 6.0 KB

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