Skip to content

Commit

Permalink
Add option to disable insertion of snippets when completing a method/…
Browse files Browse the repository at this point in the history
…function call.
  • Loading branch information
DaanDeMeyer authored and jacobdufault committed Nov 19, 2017
1 parent 8a356a7 commit 297066b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/clang_complete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ void BuildDetailString(CXCompletionString completion_string,
std::string& label,
std::string& detail,
std::string& insert,
std::vector<std::string>* parameters) {
std::vector<std::string>* parameters,
bool include_snippets) {
int num_chunks = clang_getNumCompletionChunks(completion_string);
for (int i = 0; i < num_chunks; ++i) {
CXCompletionChunkKind kind =
Expand All @@ -145,7 +146,7 @@ void BuildDetailString(CXCompletionString completion_string,
case CXCompletionChunk_Optional: {
CXCompletionString nested =
clang_getCompletionChunkCompletionString(completion_string, i);
BuildDetailString(nested, label, detail, insert, parameters);
BuildDetailString(nested, label, detail, insert, parameters, include_snippets);
break;
}

Expand All @@ -154,7 +155,8 @@ void BuildDetailString(CXCompletionString completion_string,
ToString(clang_getCompletionChunkText(completion_string, i));
parameters->push_back(text);
detail += text;
insert += "${" + std::to_string(parameters->size()) + ":" + text + "}";
// Add parameter declarations as snippets if enabled
if (include_snippets) { insert += "${" + std::to_string(parameters->size()) + ":" + text + "}"; }
break;
}

Expand Down Expand Up @@ -195,6 +197,8 @@ void BuildDetailString(CXCompletionString completion_string,
case CXCompletionChunk_LeftParen:
detail += "(";
insert += "(";
// Put cursor between parentheses if snippets are not enabled
if (!include_snippets) { insert += "$1"; }
break;
case CXCompletionChunk_RightParen:
detail += ")";
Expand Down Expand Up @@ -226,7 +230,8 @@ void BuildDetailString(CXCompletionString completion_string,
break;
case CXCompletionChunk_Comma:
detail += ", ";
insert += ", ";
// Only put comma's between parentheses if snippets are enabled
if (include_snippets) { insert += ", "; }
break;
case CXCompletionChunk_Colon:
detail += ":";
Expand All @@ -247,6 +252,8 @@ void BuildDetailString(CXCompletionString completion_string,
break;
}
}

insert += "$0";
}

void TryEnsureDocumentParsed(ClangCompleteManager* manager,
Expand Down Expand Up @@ -405,8 +412,9 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
BuildDetailString(result.CompletionString, ls_completion_item.label,
ls_completion_item.detail,
ls_completion_item.insertText,
&ls_completion_item.parameters_);
ls_completion_item.insertText += "$0";
&ls_completion_item.parameters_,
completion_manager->config_->enableSnippetInsertion);

ls_completion_item.documentation = ToString(
clang_getCompletionBriefComment(result.CompletionString));
ls_completion_item.sortText =
Expand Down
6 changes: 5 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ struct Config {

// Version of the client.
int clientVersion = 0;

// If true parameter declarations are included in code completion when calling a function or method
bool enableSnippetInsertion = true;
};
MAKE_REFLECT_STRUCT(Config,
cacheDirectory,
Expand Down Expand Up @@ -93,4 +96,5 @@ MAKE_REFLECT_STRUCT(Config,

codeLensOnLocalVariables,

clientVersion);
clientVersion,
enableSnippetInsertion);
5 changes: 5 additions & 0 deletions vscode-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@
"type": "string",
"default": "rgba(18, 18, 18, 0.3)",
"description": "css styling to apply to the background when the code region has been disabled by the preprocessor in a dark theme."
},
"cquery.completion.enableSnippetInsertion": {
"type": "boolean",
"default": true,
"description": "If true, parameter declarations are inserted as snippets in function/method call arguments when completing a function/method call"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions vscode-client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ function getClientConfig(context: vscode.ExtensionContext) {
diagnosticsOnParse: config.get('diagnostics.onParse'),
diagnosticsOnCodeCompletion: config.get('diagnostics.onCodeCompletion'),
codeLensOnLocalVariables: config.get('codeLens.onLocalVariables'),
enableSnippetInsertion: config.get('completion.enableSnippetInsertion')
}

if (!clientConfig.cacheDirectory) {
Expand Down

0 comments on commit 297066b

Please sign in to comment.