clang 20.0.0git
|
Code completion involves taking an (incomplete) source file, along with knowledge of where the user is actively editing that file, and suggesting syntactically- and semantically-valid constructs that the user might want to use at that particular point in the source code. More...
Classes | |
struct | CXCompletionResult |
A single result of code completion. More... | |
struct | CXCodeCompleteResults |
Contains the results of code-completion. More... | |
Typedefs | |
typedef void * | CXCompletionString |
A semantic string that describes a code-completion result. | |
Functions | |
CINDEX_LINKAGE enum CXCompletionChunkKind | clang_getCompletionChunkKind (CXCompletionString completion_string, unsigned chunk_number) |
Determine the kind of a particular chunk within a completion string. | |
CINDEX_LINKAGE CXString | clang_getCompletionChunkText (CXCompletionString completion_string, unsigned chunk_number) |
Retrieve the text associated with a particular chunk within a completion string. | |
CINDEX_LINKAGE CXCompletionString | clang_getCompletionChunkCompletionString (CXCompletionString completion_string, unsigned chunk_number) |
Retrieve the completion string associated with a particular chunk within a completion string. | |
CINDEX_LINKAGE unsigned | clang_getNumCompletionChunks (CXCompletionString completion_string) |
Retrieve the number of chunks in the given code-completion string. | |
CINDEX_LINKAGE unsigned | clang_getCompletionPriority (CXCompletionString completion_string) |
Determine the priority of this code completion. | |
CINDEX_LINKAGE enum CXAvailabilityKind | clang_getCompletionAvailability (CXCompletionString completion_string) |
Determine the availability of the entity that this code-completion string refers to. | |
CINDEX_LINKAGE unsigned | clang_getCompletionNumAnnotations (CXCompletionString completion_string) |
Retrieve the number of annotations associated with the given completion string. | |
CINDEX_LINKAGE CXString | clang_getCompletionAnnotation (CXCompletionString completion_string, unsigned annotation_number) |
Retrieve the annotation associated with the given completion string. | |
CINDEX_LINKAGE CXString | clang_getCompletionParent (CXCompletionString completion_string, enum CXCursorKind *kind) |
Retrieve the parent context of the given completion string. | |
CINDEX_LINKAGE CXString | clang_getCompletionBriefComment (CXCompletionString completion_string) |
Retrieve the brief documentation comment attached to the declaration that corresponds to the given completion string. | |
CINDEX_LINKAGE CXCompletionString | clang_getCursorCompletionString (CXCursor cursor) |
Retrieve a completion string for an arbitrary declaration or macro definition cursor. | |
CINDEX_LINKAGE unsigned | clang_getCompletionNumFixIts (CXCodeCompleteResults *results, unsigned completion_index) |
Retrieve the number of fix-its for the given completion index. | |
CINDEX_LINKAGE CXString | clang_getCompletionFixIt (CXCodeCompleteResults *results, unsigned completion_index, unsigned fixit_index, CXSourceRange *replacement_range) |
Fix-its that must be applied before inserting the text for the corresponding completion. | |
CINDEX_LINKAGE unsigned | clang_defaultCodeCompleteOptions (void) |
Returns a default set of code-completion options that can be passed toclang_codeCompleteAt() . | |
CINDEX_LINKAGE CXCodeCompleteResults * | clang_codeCompleteAt (CXTranslationUnit TU, const char *complete_filename, unsigned complete_line, unsigned complete_column, struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, unsigned options) |
Perform code completion at a given location in a translation unit. | |
CINDEX_LINKAGE void | clang_sortCodeCompletionResults (CXCompletionResult *Results, unsigned NumResults) |
Sort the code-completion results in case-insensitive alphabetical order. | |
CINDEX_LINKAGE void | clang_disposeCodeCompleteResults (CXCodeCompleteResults *Results) |
Free the given set of code-completion results. | |
CINDEX_LINKAGE unsigned | clang_codeCompleteGetNumDiagnostics (CXCodeCompleteResults *Results) |
Determine the number of diagnostics produced prior to the location where code completion was performed. | |
CINDEX_LINKAGE CXDiagnostic | clang_codeCompleteGetDiagnostic (CXCodeCompleteResults *Results, unsigned Index) |
Retrieve a diagnostic associated with the given code completion. | |
CINDEX_LINKAGE unsigned long long | clang_codeCompleteGetContexts (CXCodeCompleteResults *Results) |
Determines what completions are appropriate for the context the given code completion. | |
CINDEX_LINKAGE enum CXCursorKind | clang_codeCompleteGetContainerKind (CXCodeCompleteResults *Results, unsigned *IsIncomplete) |
Returns the cursor kind for the container for the current code completion context. | |
CINDEX_LINKAGE CXString | clang_codeCompleteGetContainerUSR (CXCodeCompleteResults *Results) |
Returns the USR for the container for the current code completion context. | |
CINDEX_LINKAGE CXString | clang_codeCompleteGetObjCSelector (CXCodeCompleteResults *Results) |
Returns the currently-entered selector for an Objective-C message send, formatted like "initWithFoo:bar:". | |
Code completion involves taking an (incomplete) source file, along with knowledge of where the user is actively editing that file, and suggesting syntactically- and semantically-valid constructs that the user might want to use at that particular point in the source code.
These data structures and routines provide support for code completion.
typedef void* CXCompletionString |
A semantic string that describes a code-completion result.
A semantic string that describes the formatting of a code-completion result as a single "template" of text that should be inserted into the source buffer when a particular code-completion result is selected. Each semantic string is made up of some number of "chunks", each of which contains some text along with a description of what that text means, e.g., the name of the entity being referenced, whether the text chunk is part of the template, or whether it is a "placeholder" that the user should replace with actual code,of a specific kind. See CXCompletionChunkKind
for a description of the different kinds of chunks.
enum CXCodeComplete_Flags |
Flags that can be passed to clang_codeCompleteAt()
to modify its behavior.
The enumerators in this enumeration can be bitwise-OR'd together to provide multiple options to clang_codeCompleteAt()
.
Describes a single piece of text within a code-completion string.
Each "chunk" within a code-completion string (CXCompletionString
) is either a piece of text with a specific "kind" that describes how that text should be interpreted by the client or is another completion string.
Enumerator | |
---|---|
CXCompletionChunk_Optional | A code-completion string that describes "optional" text that could be a part of the template (but is not required). The Optional chunk is the only kind of chunk that has a code-completion string for its representation, which is accessible via void f(int x, float y = 3.14, double z = 2.71828);
The code-completion string for this function would contain:
There are many ways to handle Optional chunks. Two simple approaches are:
|
CXCompletionChunk_TypedText | Text that a user would be expected to type to get this code-completion result. There will be exactly one "typed text" chunk in a semantic string, which will typically provide the spelling of a keyword or the name of a declaration that could be used at the current code point. Clients are expected to filter the code-completion results based on the text in this chunk. |
CXCompletionChunk_Text | Text that should be inserted as part of a code-completion result. A "text" chunk represents text that is part of the template to be inserted into user code should this particular code-completion result be selected. |
CXCompletionChunk_Placeholder | Placeholder text that should be replaced by the user. A "placeholder" chunk marks a place where the user should insert text into the code-completion template. For example, placeholders might mark the function parameters for a function declaration, to indicate that the user should provide arguments for each of those parameters. The actual text in a placeholder is a suggestion for the text to display before the user replaces the placeholder with real code. |
CXCompletionChunk_Informative | Informative text that should be displayed but never inserted as part of the template. An "informative" chunk contains annotations that can be displayed to help the user decide whether a particular code-completion result is the right option, but which is not part of the actual template to be inserted by code completion. |
CXCompletionChunk_CurrentParameter | Text that describes the current parameter when code-completion is referring to function call, message send, or template specialization. A "current parameter" chunk occurs when code-completion is providing information about a parameter corresponding to the argument at the code-completion point. For example, given a function int add(int x, int y);
and the source code |
CXCompletionChunk_LeftParen | A left parenthesis ('('), used to initiate a function call or signal the beginning of a function parameter list. |
CXCompletionChunk_RightParen | A right parenthesis (')'), used to finish a function call or signal the end of a function parameter list. |
CXCompletionChunk_LeftBracket | A left bracket ('['). |
CXCompletionChunk_RightBracket | A right bracket (']'). |
CXCompletionChunk_LeftBrace | A left brace ('{'). |
CXCompletionChunk_RightBrace | A right brace ('}'). |
CXCompletionChunk_LeftAngle | A left angle bracket ('<'). |
CXCompletionChunk_RightAngle | A right angle bracket ('>'). |
CXCompletionChunk_Comma | A comma separator (','). |
CXCompletionChunk_ResultType | Text that specifies the result type of a given result. This special kind of informative chunk is not meant to be inserted into the text buffer. Rather, it is meant to illustrate the type that an expression using the given completion string would have. |
CXCompletionChunk_Colon | A colon (':'). |
CXCompletionChunk_SemiColon | A semicolon (';'). |
CXCompletionChunk_Equal | An '=' sign. |
CXCompletionChunk_HorizontalSpace | Horizontal space (' '). |
CXCompletionChunk_VerticalSpace | Vertical space ('\n'), after which it is generally a good idea to perform indentation. |
enum CXCompletionContext |
Bits that represent the context under which completion is occurring.
The enumerators in this enumeration may be bitwise-OR'd together if multiple contexts are occurring simultaneously.
CINDEX_LINKAGE CXCodeCompleteResults * clang_codeCompleteAt | ( | CXTranslationUnit | TU, |
const char * | complete_filename, | ||
unsigned | complete_line, | ||
unsigned | complete_column, | ||
struct CXUnsavedFile * | unsaved_files, | ||
unsigned | num_unsaved_files, | ||
unsigned | options | ||
) |
Perform code completion at a given location in a translation unit.
This function performs code completion at a particular file, line, and column within source code, providing results that suggest potential code snippets based on the context of the completion. The basic model for code completion is that Clang will parse a complete source file, performing syntax checking up to the location where code-completion has been requested. At that point, a special code-completion token is passed to the parser, which recognizes this token and determines, based on the current location in the C/Objective-C/C++ grammar and the state of semantic analysis, what completions to provide. These completions are returned via a new CXCodeCompleteResults
structure.
Code completion itself is meant to be triggered by the client when the user types punctuation characters or whitespace, at which point the code-completion location will coincide with the cursor. For example, if p
is a pointer, code-completion might be triggered after the "-" and then after the ">" in p->
. When the code-completion location is after the ">", the completion results will provide, e.g., the members of the struct that "p" points to. The client is responsible for placing the cursor at the beginning of the token currently being typed, then filtering the results based on the contents of the token. For example, when code-completing for the expression p->get
, the client should provide the location just after the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the client can filter the results based on the current token text ("get"), only showing those results that start with "get". The intent of this interface is to separate the relatively high-latency acquisition of code-completion results from the filtering of results on a per-character basis, which must have a lower latency.
TU | The translation unit in which code-completion should occur. The source files for this translation unit need not be completely up-to-date (and the contents of those source files may be overridden via unsaved_files ). Cursors referring into the translation unit may be invalidated by this invocation. |
complete_filename | The name of the source file where code completion should be performed. This filename may be any file included in the translation unit. |
complete_line | The line at which code-completion should occur. |
complete_column | The column at which code-completion should occur. Note that the column should point just after the syntactic construct that initiated code completion, and not in the middle of a lexical token. |
unsaved_files | the Files that have not yet been saved to disk but may be required for parsing or code completion, including the contents of those files. The contents and name of these files (as specified by CXUnsavedFile) are copied when necessary, so the client only needs to guarantee their validity until the call to this function returns. |
num_unsaved_files | The number of unsaved file entries in unsaved_files . |
options | Extra options that control the behavior of code completion, expressed as a bitwise OR of the enumerators of the CXCodeComplete_Flags enumeration. The clang_defaultCodeCompleteOptions() function returns a default set of code-completion options. |
CXCodeCompleteResults
structure containing code-completion results, which should eventually be freed with clang_disposeCodeCompleteResults()
. If code completion fails, returns NULL. CINDEX_LINKAGE enum CXCursorKind clang_codeCompleteGetContainerKind | ( | CXCodeCompleteResults * | Results, |
unsigned * | IsIncomplete | ||
) |
Returns the cursor kind for the container for the current code completion context.
The container is only guaranteed to be set for contexts where a container exists (i.e. member accesses or Objective-C message sends); if there is not a container, this function will return CXCursor_InvalidCode.
Results | the code completion results to query |
IsIncomplete | on return, this value will be false if Clang has complete information about the container. If Clang does not have complete information, this value will be true. |
CINDEX_LINKAGE CXString clang_codeCompleteGetContainerUSR | ( | CXCodeCompleteResults * | Results | ) |
Returns the USR for the container for the current code completion context.
If there is not a container for the current context, this function will return the empty string.
Results | the code completion results to query |
CINDEX_LINKAGE unsigned long long clang_codeCompleteGetContexts | ( | CXCodeCompleteResults * | Results | ) |
Determines what completions are appropriate for the context the given code completion.
Results | the code completion results to query |
CINDEX_LINKAGE CXDiagnostic clang_codeCompleteGetDiagnostic | ( | CXCodeCompleteResults * | Results, |
unsigned | Index | ||
) |
Retrieve a diagnostic associated with the given code completion.
Results | the code completion results to query. |
Index | the zero-based diagnostic number to retrieve. |
clang_disposeDiagnostic()
. CINDEX_LINKAGE unsigned clang_codeCompleteGetNumDiagnostics | ( | CXCodeCompleteResults * | Results | ) |
Determine the number of diagnostics produced prior to the location where code completion was performed.
CINDEX_LINKAGE CXString clang_codeCompleteGetObjCSelector | ( | CXCodeCompleteResults * | Results | ) |
Returns the currently-entered selector for an Objective-C message send, formatted like "initWithFoo:bar:".
Only guaranteed to return a non-empty string for CXCompletionContext_ObjCInstanceMessage and CXCompletionContext_ObjCClassMessage.
Results | the code completion results to query |
CINDEX_LINKAGE unsigned clang_defaultCodeCompleteOptions | ( | void | ) |
Returns a default set of code-completion options that can be passed toclang_codeCompleteAt()
.
CINDEX_LINKAGE void clang_disposeCodeCompleteResults | ( | CXCodeCompleteResults * | Results | ) |
Free the given set of code-completion results.
CINDEX_LINKAGE CXString clang_getCompletionAnnotation | ( | CXCompletionString | completion_string, |
unsigned | annotation_number | ||
) |
Retrieve the annotation associated with the given completion string.
completion_string | the completion string to query. |
annotation_number | the 0-based index of the annotation of the completion string. |
annotation_number
, or a NULL string if that annotation is not available. CINDEX_LINKAGE enum CXAvailabilityKind clang_getCompletionAvailability | ( | CXCompletionString | completion_string | ) |
Determine the availability of the entity that this code-completion string refers to.
completion_string | The completion string to query. |
CINDEX_LINKAGE CXString clang_getCompletionBriefComment | ( | CXCompletionString | completion_string | ) |
Retrieve the brief documentation comment attached to the declaration that corresponds to the given completion string.
CINDEX_LINKAGE CXCompletionString clang_getCompletionChunkCompletionString | ( | CXCompletionString | completion_string, |
unsigned | chunk_number | ||
) |
Retrieve the completion string associated with a particular chunk within a completion string.
completion_string | the completion string to query. |
chunk_number | the 0-based index of the chunk in the completion string. |
chunk_number
. CINDEX_LINKAGE enum CXCompletionChunkKind clang_getCompletionChunkKind | ( | CXCompletionString | completion_string, |
unsigned | chunk_number | ||
) |
Determine the kind of a particular chunk within a completion string.
completion_string | the completion string to query. |
chunk_number | the 0-based index of the chunk in the completion string. |
chunk_number
. CINDEX_LINKAGE CXString clang_getCompletionChunkText | ( | CXCompletionString | completion_string, |
unsigned | chunk_number | ||
) |
Retrieve the text associated with a particular chunk within a completion string.
completion_string | the completion string to query. |
chunk_number | the 0-based index of the chunk in the completion string. |
chunk_number
. CINDEX_LINKAGE CXString clang_getCompletionFixIt | ( | CXCodeCompleteResults * | results, |
unsigned | completion_index, | ||
unsigned | fixit_index, | ||
CXSourceRange * | replacement_range | ||
) |
Fix-its that must be applied before inserting the text for the corresponding completion.
By default, clang_codeCompleteAt() only returns completions with empty fix-its. Extra completions with non-empty fix-its should be explicitly requested by setting CXCodeComplete_IncludeCompletionsWithFixIts.
For the clients to be able to compute position of the cursor after applying fix-its, the following conditions are guaranteed to hold for replacement_range of the stored fix-its:
The intuition is that provided fix-its change code around the identifier we complete, but are not allowed to touch the identifier itself or the completion point. One example of completions with corrections are the ones replacing '.' with '->' and vice versa:
std::unique_ptr<std::vector<int>> vec_ptr; In 'vec_ptr.^', one of the completions is 'push_back', it requires replacing '.' with '->'. In 'vec_ptr->^', one of the completions is 'release', it requires replacing '->' with '.'.
results | The structure keeping all completion results |
completion_index | The index of the completion |
fixit_index | The index of the fix-it for the completion at completion_index |
replacement_range | The fix-it range that must be replaced before the completion at completion_index can be applied |
CINDEX_LINKAGE unsigned clang_getCompletionNumAnnotations | ( | CXCompletionString | completion_string | ) |
Retrieve the number of annotations associated with the given completion string.
completion_string | the completion string to query. |
CINDEX_LINKAGE unsigned clang_getCompletionNumFixIts | ( | CXCodeCompleteResults * | results, |
unsigned | completion_index | ||
) |
Retrieve the number of fix-its for the given completion index.
Calling this makes sense only if CXCodeComplete_IncludeCompletionsWithFixIts option was set.
results | The structure keeping all completion results |
completion_index | The index of the completion |
CINDEX_LINKAGE CXString clang_getCompletionParent | ( | CXCompletionString | completion_string, |
enum CXCursorKind * | kind | ||
) |
Retrieve the parent context of the given completion string.
The parent context of a completion string is the semantic parent of the declaration (if any) that the code completion represents. For example, a code completion for an Objective-C method would have the method's class or protocol as its context.
completion_string | The code completion string whose parent is being queried. |
kind | DEPRECATED: always set to CXCursor_NotImplemented if non-NULL. |
CINDEX_LINKAGE unsigned clang_getCompletionPriority | ( | CXCompletionString | completion_string | ) |
Determine the priority of this code completion.
The priority of a code completion indicates how likely it is that this particular completion is the completion that the user will select. The priority is selected by various internal heuristics.
completion_string | The completion string to query. |
CINDEX_LINKAGE CXCompletionString clang_getCursorCompletionString | ( | CXCursor | cursor | ) |
Retrieve a completion string for an arbitrary declaration or macro definition cursor.
cursor | The cursor to query. |
CINDEX_LINKAGE unsigned clang_getNumCompletionChunks | ( | CXCompletionString | completion_string | ) |
Retrieve the number of chunks in the given code-completion string.
CINDEX_LINKAGE void clang_sortCodeCompletionResults | ( | CXCompletionResult * | Results, |
unsigned | NumResults | ||
) |
Sort the code-completion results in case-insensitive alphabetical order.
Results | The set of results to sort. |
NumResults | The number of results in Results . |