@@ -256,9 +256,17 @@ struct TextDocumentCompletionHandler : MessageHandler {
256256 std::string character = *request->params .context ->triggerCharacter ;
257257 char preceding_index = request->params .position .character - 2 ;
258258
259+ // If the character is '"' or '<', make sure the line is start with '#'.
260+ if (character == " \" " || character == " <" ) {
261+ size_t i = 0 ;
262+ while (i < buffer_line.size () && isspace (buffer_line[i]))
263+ ++i;
264+ if (i >= buffer_line.size () || buffer_line[i] != ' #' )
265+ did_fail_check = true ;
266+ }
259267 // If the character is > or : and we are at the start of the line, do not
260268 // show completion results.
261- if ((character == " >" || character == " :" ) && preceding_index < 0 ) {
269+ else if ((character == " >" || character == " :" ) && preceding_index < 0 ) {
262270 did_fail_check = true ;
263271 }
264272 // If the character is > but - does not preced it, or if it is : and :
@@ -283,7 +291,11 @@ struct TextDocumentCompletionHandler : MessageHandler {
283291 &existing_completion);
284292 }
285293
286- if (ShouldRunIncludeCompletion (buffer_line)) {
294+ bool yes;
295+ std::string surround, prefix;
296+ std::tie (yes, surround, prefix) = ShouldRunIncludeCompletion (buffer_line);
297+
298+ if (yes) {
287299 Out_TextDocumentComplete out;
288300 out.id = request->id ;
289301
@@ -296,19 +308,26 @@ struct TextDocumentCompletionHandler : MessageHandler {
296308 include_complete->completion_items .end ());
297309 if (lock)
298310 lock.unlock ();
299-
300- // Update textEdit params.
301- for (lsCompletionItem& item : out.result .items ) {
302- item.textEdit ->range .start .line = request->params .position .line ;
303- item.textEdit ->range .start .character = 0 ;
304- item.textEdit ->range .end .line = request->params .position .line ;
305- item.textEdit ->range .end .character = (int )buffer_line.size ();
306- }
307311 }
308312
309- TrimInPlace (buffer_line);
310- FilterAndSortCompletionResponse (&out, buffer_line,
313+ FilterAndSortCompletionResponse (&out, prefix,
311314 config->completion .filterAndSort );
315+
316+ auto decorator = [&](std::string& text) {
317+ std::string result = " #include " ;
318+ result += surround[0 ] + text + surround[1 ];
319+ text = result;
320+ };
321+ LOG_S (INFO) << " DEBUG prefix " << prefix;
322+ for (lsCompletionItem& item : out.result .items ) {
323+ item.textEdit ->range .start .line = request->params .position .line ;
324+ item.textEdit ->range .start .character = 0 ;
325+ item.textEdit ->range .end .line = request->params .position .line ;
326+ item.textEdit ->range .end .character = (int )buffer_line.size ();
327+ decorator (item.textEdit ->newText );
328+ decorator (item.label );
329+ }
330+
312331 QueueManager::WriteStdout (IpcId::TextDocumentCompletion, out);
313332 } else {
314333 // If existing completion is empty, dont return clang-based completion
0 commit comments