sublimehq / sublime_text

Issue tracker for Sublime Text
https://www.sublimetext.com
807 stars 39 forks source link

Lack of autocomplete on indexed C headers #5031

Open frithrah opened 2 years ago

frithrah commented 2 years ago

Description of the bug

Autocomplete is not suggesting functions from indexed C headers.

Steps to reproduce

  1. Start in safe mode
  2. Create a new project
  3. Add a project folder for a library include directory so the files are indexed - I'm using SDL2 (https://github.com/libsdl-org/SDL/tree/main/include)
    "folders":
    [
        {
            "path": "."
        },
        {
            "name": "SDL",
            "path": "/usr/include/SDL2"
        }
    ]
  4. Check Indexing Status... to ensure symbols from the headers have been catalogued
  5. In a new .c file, start typing a function from the included headers - e.g. SDL_Cond

Expected behavior

Autocomplete suggests functions from included header files.

Actual behavior

Autocomplete does not suggest functions from included headers. If I type the function manually, goto Definition and references work so the indexes are good - all that's missing is the autocomplete.

Sublime Text build number

4122

Operating system & version

openSUSE Tumbleweed

(Linux) Desktop environment and/or window manager

Xfce

Additional information

No response

OpenGL context information

No response

deathaxe commented 2 years ago

That's what ST's auto-completion is designed to work like.

It only completes tokens from global index if they appear in the buffer at least once. So if a function foo() declared in a header file is indexed, ST will auto-complete it only, if it finds a foo() function call in the open document.

The reason why it works like that is performance. ST isn't able to filter indexed tokens semantically and would display all kinds of irrelevant stuff from any indexed file and syntax. Depending on the open project many many many items would be suggested which would slow down ST for nothing.

frithrah commented 2 years ago

Thanks for replying but I don't think that's the case. If I add a folder to the actual .c files (not just the headers) then autocomplete will suggest functions from those files even if it's the first time I've used them*. This is correct and good and fast. It's just references from headers only that are missed (probably for a good reason but I don't know what).

deathaxe commented 2 years ago

Here's a small example to illustrate how it works without any LSP or code intelligence plugins installed.

Animation

See how the first time fun is typed, nothing is completed in debug.c, while fun1 is suggested once it exists. But fun2 is still not suggested (as it does not yet exist).

Once fun1 is found and suggested in completions ST also shows the 2 locatations of definition.

As headers (.h) and sources (.c) use the same syntax and therefore the same indexing rules they are treated equally by ST's indexer.

frithrah commented 2 years ago

Hmmm. Well that's interesting. Here's what I'm seeing...

  1. Start Sublime in safe mode
  2. New project
  3. Add external SDL folder
  4. New file main.c
  5. Type: SDL_Co
  6. SDL functions beginning Co... appear

st-indexing

What are we doing differently here? :)

For the record, this is what I want and expect, it just doesn't do this when only the headers are added (as would be the case when using most shared libraries).

frithrah commented 2 years ago

Here's an even clearer example. It demonstrates that autocomplete isn't picked up from a header file but is when the c body is added.

This example uses a minimal header and source file from the PCG project. Just grab: pcg_basic.h and pcg_basic.c.

  1. Start in safe mode passing an empty directory
  2. Copy pcg_basic.h into the empty directory
  3. Note the Indexing Status cataloguing the header file index "Simple" collated in 0.00s from 1 files index "Simple" is using 4096 bytes for 10 symbols across 10 locations
  4. New file: main.c
  5. Insert basic main function
  6. Within main, type: pcg
  7. No available completions :(
  8. Copy in the source file, pcg_basic.c
  9. Note the Indexing Status adding in the c symbols index "Simple" collated in 0.00s from 2 files index "Simple" is using 4096 bytes for 16 symbols across 22 locations
  10. Within main, type: pcg
  11. Autocomplete :)

It would be brilliant to get autocomplete when only the header is indexed so it works when using shared libraries.