Bug 384710 - "Magic function completion" ignores existing text
Summary: "Magic function completion" ignores existing text
Status: RESOLVED FIXED
Alias: None
Product: kdevelop
Classification: Applications
Component: Language Support: CPP (Clang-based) (show other bugs)
Version: 5.1.2
Platform: Gentoo Packages Linux
: NOR normal
Target Milestone: ---
Assignee: kdevelop-bugs-null
URL:
Keywords:
: 363057 385217 (view as bug list)
Depends on:
Blocks:
 
Reported: 2017-09-14 16:00 UTC by TheComet
Modified: 2018-10-23 11:59 UTC (History)
5 users (show)

See Also:
Latest Commit:
Version Fixed In: 5.4.0
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description TheComet 2017-09-14 16:00:45 UTC
Overview
--------
I forget the exact name of this feature, but when you declare a function or a member function in a header file and then partially type it in an implementation file, the editor will "magically" produce suggestions for autocompleting the declared function. In the old parser, this would replace the text you had already typed with the new completion. In the new parser, it ignores any text you have typed and just fills in the function, which in a lot of cases is just wrong.

Steps to Reproduce
------------------
1) Create a header file with the following content

  void Foo(int thing);
  struct Bar
  {
      void Method(int thing);
  };

2) Create a .cpp file and type: "void Foo"
3) Wait, because the parser is now really slow
4) Finally, a suggestion for completing the "Foo" function pops up. Accept it.
5) Do the same for "Method" by typing "void Method" and accepting the completion.

Actual Results
--------------
The resulting completions ignore the already typed "void" keyword and simply insert the signatures, such that "void" now appears twice:

  void void Foo(int thing)
  {
  }

  void void Bar::Method(int thing)
  {
  }

If a project wishes to format the return type such that it appears on a new line, then typing "void <new line> Foo" and accepting the suggestion produces this incorrect completion:

  void 
  void Foo(int thing)
  {
  }

An even worse example of this: 
1) Begin typing "void Foo(int "
2) Press ctrl+space
3) Select "implement method Foo"
4) The completion creates this terrible snippet:

  Foo(int void Foo(int thing)
  {
  }
  )

Expected Results
----------------
The parser should be aware of the code that has already been typed and remove/adjust it as necessary.

For the first example I expect the code to look like this after completion:

  void Foo(int thing)
  {
  }

  void Bar::Method(int thing)
  {
  }

For the second example:

  void 
  Foo(int thing)
  {
  }

And for the third example:

  void Foo(int thing)
  {
  }

Build Date & Platform
---------------------
Ever since the clang based parser was introduced.
Comment 1 Francis Herne 2017-09-18 20:26:44 UTC
Yes, I've noticed this also; it's still a problem with the 5.2 branch.
Comment 2 Sven Brauch 2017-09-18 20:28:29 UTC
I'm fairly sure I've seen this before 5.2, i.e. like always.
Comment 3 Amish Naidu 2018-10-19 16:53:14 UTC
*** Bug 363057 has been marked as a duplicate of this bug. ***
Comment 4 Amish Naidu 2018-10-19 16:56:22 UTC
*** Bug 385217 has been marked as a duplicate of this bug. ***
Comment 5 Amish Naidu 2018-10-23 07:55:13 UTC
Git commit 4f2fc9e32452b3dc7f017d57c0bcc98b73cb0370 by Amish Naidu.
Committed on 23/10/2018 at 07:54.
Pushed by anaidu into branch 'master'.

Replace leading typed text when completing function implementation

Summary:
When executing function-implementation auto-completion, this will now
replace and leading typed text that matches the proposed completion
instead of duplicating it.

Test Plan:
```
int Foo(int thing);
struct Bar
{
  void Method(int thing);
}
```
Start typing `int Foo` or `Bar::Meth` and then execute the offered completion.

Reviewers: #kdevelop, mwolff

Reviewed By: #kdevelop, mwolff

Subscribers: kfunk, brauch, mwolff, apol, kdevelop-devel

Tags: #kdevelop

Differential Revision: https://phabricator.kde.org/D16326

M  +6    -0    kdevplatform/language/duchain/stringhelpers.cpp
M  +5    -0    kdevplatform/language/duchain/stringhelpers.h
M  +16   -1    plugins/clang/codecompletion/context.cpp
M  +24   -0    plugins/clang/tests/test_codecompletion.cpp

https://commits.kde.org/kdevelop/4f2fc9e32452b3dc7f017d57c0bcc98b73cb0370
Comment 6 RJVB 2018-10-23 11:59:48 UTC
Applies and builds in the 5.3 branch too. Can't yet say if it also has the desired effect but why wouldn't it... So thanks, it was quite annoying for a minor nuisance.