Skip to content

Add path completion support inside require calls #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2023

Conversation

paracycle
Copy link
Member

@paracycle paracycle commented Aug 22, 2022

Motivation

I got inspired by the recently released Markdown Language Server doing completion for references to other files. I realized that we can match require strings against a list of files in the $LOAD_PATH and suggest completions like that.

Note: This is still missing some of the functionality, but since it is gated behind a feature flag that should be turned on by the VSCode extension, I think it is safe to land. The things that are missing are:

  1. There is no support for requires that start with a . or .. which make them relative requires.
  2. There is no support for load paths that may be added by the application. For example, Rails applications add Rails.root / "lib" and Rails.root / "app/models", etc to the load paths, which this extension will not be able to see (since we don't boot the application we are running inside).
  3. There is no support for require_relative.

Implementation

  1. Register a completion provider that is triggered on the / character
  2. Handle the completion request and process it if (and only if) it is originating from the string argument of a require call.
  3. Match the completion prefix to the list of paths in $LOAD_PATH and offer completions.

Automated Tests

Added a small automated test

Manual Tests

  1. Open the https://github.com/Shopify/vscode-ruby-lsp project.
  2. Add a
            "completion": {
              "description": "Enable code completion features",
              "type": "boolean",
              "default": true
            }
    feature inside the package.json file and a:
             "completion": true
    default
  3. Run the project
  4. Open the ruby-lsp project in the Extension Host window.
  5. Start typing some requires and see autocompletion for paths.

@paracycle paracycle force-pushed the uk-add-path-completion branch 3 times, most recently from 01e6e47 to 9489f44 Compare August 22, 2022 16:16
@vinistock
Copy link
Member

This is awesome ❤️ . I wonder if it would be worth finding files in the current directory as well? For example, if you create a brand new file and want to require it, it won't be in the load path yet.

@paracycle paracycle force-pushed the uk-add-path-completion branch from 9489f44 to 74e9cc4 Compare February 10, 2023 15:47
@paracycle paracycle changed the title [RFC] Add path completion support inside require calls Add path completion support inside require calls Feb 10, 2023
@paracycle paracycle marked this pull request as ready for review February 10, 2023 15:48
@paracycle paracycle requested a review from a team as a code owner February 10, 2023 15:48
@paracycle
Copy link
Member Author

@Morriar and I reworked this PR so that it uses a more efficient Trie (which is also called a Prefix Tree) data structure to index files in the load path and brought it up to work against the changes in latest main.

@vinistock Currently we reindex the files on the load path for every request to completion, so we would be able to discover any newly added files. I know this is not the most efficient way of doing it, but based on my usage testing the responsiveness seems to be just fine for now.

@paracycle paracycle force-pushed the uk-add-path-completion branch from 74e9cc4 to 0c7537b Compare February 10, 2023 16:37
@paracycle paracycle force-pushed the uk-add-path-completion branch from 0c7537b to c776598 Compare February 13, 2023 16:20
@paracycle paracycle requested a review from Morriar February 13, 2023 16:23
@paracycle paracycle force-pushed the uk-add-path-completion branch 2 times, most recently from 05f14b9 to 98db569 Compare February 13, 2023 19:51
Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works really well! Some comments about the code structure

Co-authored-by: Ufuk Kayserilioglu <ufuk@paralaus.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@paracycle paracycle force-pushed the uk-add-path-completion branch from 98db569 to 4abb52c Compare February 13, 2023 22:48
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Co-authored-by: Ufuk Kayserilioglu <ufuk@paralaus.com>
@paracycle paracycle force-pushed the uk-add-path-completion branch from 4abb52c to 076ebc6 Compare February 13, 2023 22:51
@paracycle paracycle requested a review from vinistock February 13, 2023 23:10
Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing!! 🚀

@paracycle paracycle merged commit d9307ab into main Feb 14, 2023
@paracycle paracycle deleted the uk-add-path-completion branch February 14, 2023 15:18
@shopify-shipit shopify-shipit bot temporarily deployed to production February 15, 2023 20:50 Inactive
andyw8 pushed a commit to andyw8/ruby-lsp that referenced this pull request Mar 2, 2024
…ypescript-eslint/eslint-plugin-5.40.1

Bump @typescript-eslint/eslint-plugin from 5.40.0 to 5.40.1
klaaspieter pushed a commit to klaaspieter/ruby-lsp that referenced this pull request Feb 25, 2025
* Remove mention of `ruby_lsp_rails.server` from readme

* Fix link in readme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants