diff --git a/clang-tools-extra/docs/clangd/SimpleExample.rst b/clang-tools-extra/docs/clangd/SimpleExample.rst new file mode 100644 --- /dev/null +++ b/clang-tools-extra/docs/clangd/SimpleExample.rst @@ -0,0 +1,110 @@ +===================== +Simple example clangd +===================== + +clangd is not designed to be used in command line. +However, we are providing this example to show how it works. + +The following file (ex: commands.json) represents a list of commands +to get a completion suggestion. + +.. code-block:: json + + { + "jsonrpc": "2.0", + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "completion": { + "completionItem": { + "snippetSupport": true + } + } + } + }, + "trace": "off" + } + } + --- + { + "jsonrpc": "2.0", + "method": "textDocument/didOpen", + "params": { + "textDocument": { + "uri": "test:///main.cpp", + "languageId": "cpp", + "version": 1, + "text": "int func_with_args(int a, int b);\nint main() {\nfunc_with\n}" + } + } + } + --- + { + "jsonrpc": "2.0", + "id": 1, + "method": "textDocument/completion", + "params": { + "textDocument": { + "uri": "test:///main.cpp" + }, + "position": { + "line": 2, + "character": 7 + } + } + } + --- + { + "jsonrpc": "2.0", + "id": 4, + "method": "shutdown" + } + --- + { + "jsonrpc": "2.0", + "method": "exit" + } + + +To run all the commands at once: + +.. code-block:: shell + + clangd -lit-test < commands.json + +This example will try to get the completion on line 2, column 7. +Here is a subset of the returned information: + +.. code-block:: json + + [...] + "items": [ + { + "detail": "int", + "filterText": "func_with_args", + "insertText": "func_with_args(${1:int a}, ${2:int b})", + "insertTextFormat": 2, + "kind": 3, + "label": " func_with_args(int a, int b)", + "score": 9.9000005722045898, + "sortText": "3ee19999func_with_args", + "textEdit": { + "newText": "func_with_args(${1:int a}, ${2:int b})", + "range": { + "end": { + "character": 7, + "line": 2 + }, + "start": { + "character": 0, + "line": 2 + } + } + } + } + [...] + +`newText` shows that clangd is able to find a suggestion for the completion and +where to insert it. diff --git a/clang-tools-extra/docs/clangd/index.rst b/clang-tools-extra/docs/clangd/index.rst --- a/clang-tools-extra/docs/clangd/index.rst +++ b/clang-tools-extra/docs/clangd/index.rst @@ -8,6 +8,7 @@ Installation Features Configuration + SimpleExample What is clangd? ===============