rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

feat: Allow excluding specific traits from completion

Open ChayimFriedman2 opened this issue 1 year ago • 2 comments

To be accurate, only their methods are excluded, the trait themselves are still available.

I also excluded a bunch of std traits by default. Some less opinionated, like AsRef, which should never be used directly except in generic scenarios (and won't be excluded there), some more opinionated, like the ops traits, which I know some users sometimes want to use directly. Either way it's configurable.

It should be pretty easy to extend support to excluding only specific methods, but I didn't do that currently.

Traits configured to be excluded are resolved in each completion request from scratch. If this proves too expensive, it is easy enough to cache them in the DB.

Leave #17477 open, as per @dtolnay request.

ChayimFriedman2 avatar Sep 24 '24 16:09 ChayimFriedman2

:umbrella: The latest upstream changes (presumably #18167) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Sep 30 '24 08:09 bors

:umbrella: The latest upstream changes (presumably #18291) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Oct 14 '24 13:10 bors

So, I am finally coming back to this (apologies, I forgot about it!) and have since seen the PR to IJ that added similar things https://github.com/intellij-rust/intellij-rust/pull/9123.

They have apparently two types of exclusion, they allow excluding completions of only the traits methods, or the methods as well as the trait itself. I think we wanna stick with what we have here for now, but make the config more general. That is instead of completion_autoimport_excludeTraits just completion_autoimport_exclude and then have that take a map instead of a list where the value is the kind of exclusion (which only allows one kind for now). That allows us to re-use this for more things. Similar treatment for completion_excludeTraits -> completion_exclude. Thoughts on that?

Veykril avatar Dec 24 '24 08:12 Veykril

Relevant issue https://github.com/rust-lang/rust-analyzer/issues/13786#issuecomment-1363888390

Veykril avatar Dec 31 '24 16:12 Veykril

This doesn't seem to be working. On a simple project with color_eyre as a dependency, I still see suggestions for it image

I did export RA_LOG=info in the root of my repo, then launched my editor. Looking at the log file for rust-analyzer, I can see that the client sent my config with the desired completion.excludeTraits config. I also shows that the server version is 9fc6b43 2025-01-07, which came later than the release that incorporated this feature.

Any pointers?

rust-analyzer logs
2025-01-10T10:39:18.514172767-03:00  INFO server version 1.84.0 (9fc6b43 2025-01-07) will start
2025-01-10T10:39:18.514521578-03:00  INFO InitializeParams: {"processId":136243,"workDoneToken":"1","workspaceFolders":[{"uri":"file:///home/doom/git/unmaykr-aftermath/playground","name":"/home/doom/git/unmaykr-aftermath/playground"}],"capabilities":{"workspace":{"workspaceEdit":{"resourceOperations":["rename","create","delete"]},"didChangeConfiguration":{"dynamicRegistration":false},"applyEdit":true,"workspaceFolders":true,"semanticTokens":{"refreshSupport":true},"configuration":true,"inlayHint":{"refreshSupport":true},"symbol":{"dynamicRegistration":false,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}},"didChangeWatchedFiles":{"dynamicRegistration":false,"relativePatternSupport":true}},"general":{"positionEncodings":["utf-16"]},"textDocument":{"rename":{"prepareSupport":true,"dynamicRegistration":true},"semanticTokens":{"multilineTokenSupport":false,"overlappingTokenSupport":true,"tokenTypes":["namespace","type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator","decorator"],"serverCancelSupport":false,"tokenModifiers":["declaration","definition","readonly","static","deprecated","abstract","async","modification","documentation","defaultLibrary"],"dynamicRegistration":false,"requests":{"range":false,"full":{"delta":true}},"augmentsSyntaxTokens":true,"formats":["relative"]},"completion":{"insertTextMode":1,"completionList":{"itemDefaults":["editRange","insertTextFormat","insertTextMode","data"]},"dynamicRegistration":false,"contextSupport":false,"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]},"completionItem":{"preselectSupport":true,"insertReplaceSupport":true,"labelDetailsSupport":true,"deprecatedSupport":true,"commitCharactersSupport":true,"snippetSupport":true,"resolveSupport":{"properties":["documentation","detail","additionalTextEdits"]},"tagSupport":{"valueSet":[1]},"insertTextModeSupport":{"valueSet":[1,2]},"documentationFormat":["markdown","plaintext"]}},"inlayHint":{"dynamicRegistration":true,"resolveSupport":{"properties":["textEdits","tooltip","location","command"]}},"definition":{"linkSupport":true,"dynamicRegistration":true},"callHierarchy":{"dynamicRegistration":false},"publishDiagnostics":{"relatedInformation":true,"dataSupport":true,"tagSupport":{"valueSet":[1,2]}},"implementation":{"linkSupport":true},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"documentSymbol":{"dynamicRegistration":false,"hierarchicalDocumentSymbolSupport":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}},"formatting":{"dynamicRegistration":true},"references":{"dynamicRegistration":false},"signatureHelp":{"dynamicRegistration":false,"signatureInformation":{"parameterInformation":{"labelOffsetSupport":true},"activeParameterSupport":true,"documentationFormat":["markdown","plaintext"]}},"synchronization":{"willSaveWaitUntil":true,"didSave":true,"dynamicRegistration":false,"willSave":true},"typeDefinition":{"linkSupport":true},"foldingRange":{"dynamicRegistration":false,"lineFoldingOnly":true},"rangeFormatting":{"dynamicRegistration":true},"declaration":{"linkSupport":true},"diagnostic":{"dynamicRegistration":false},"codeAction":{"resolveSupport":{"properties":["edit"]},"isPreferredSupport":true,"dynamicRegistration":true,"dataSupport":true,"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}}},"documentHighlight":{"dynamicRegistration":false}},"experimental":{"codeActionGroup":true,"snippetTextEdit":true,"serverStatusNotification":true,"hoverRange":true,"colorDiagnosticOutput":true,"commands":{"commands":["rust-analyzer.runSingle","rust-analyzer.showReferences","rust-analyzer.gotoLocation","editor.action.triggerParameterHints"]},"hoverActions":true,"ssr":true},"window":{"showDocument":{"support":true},"workDoneProgress":true,"showMessage":{"messageActionItem":{"additionalPropertiesSupport":false}}}},"trace":"off","initializationOptions":{"check":{"command":"clippy"},"rustfmt":{"extraArgs":["+nightly"]},"cargo":{"allFeatures":true},"completion":{"excludeTraits":["core::borrow::Borrow","core::borrow::BorrowMut","std::borrow::Borrow","std::borrow::BorrowMut","owo_colors::OwoColorize","color_eyre::owo_colors::OwoColorize"]}},"rootUri":"file:///home/doom/git/unmaykr-aftermath/playground","rootPath":"/home/doom/git/unmaykr-aftermath/playground","clientInfo":{"name":"Neovim","version":"0.10.3"}}
2025-01-10T10:39:18.514697365-03:00  INFO Client 'Neovim' 0.10.3
2025-01-10T10:39:18.514786612-03:00  INFO updating config from JSON: {
  "check": {
    "command": "clippy"
  },
  "rustfmt": {
    "extraArgs": [
      "+nightly"
    ]
  },
  "cargo": {
    "allFeatures": true
  },
  "completion": {
    "excludeTraits": [
      "core::borrow::Borrow",
      "core::borrow::BorrowMut",
      "std::borrow::Borrow",
      "std::borrow::BorrowMut",
      "owo_colors::OwoColorize",
      "color_eyre::owo_colors::OwoColorize"
    ]
  }
}
Nvim config
return {
  {
    "AstroNvim/astrolsp",
    opts = function(_, opts)
      opts.config.rust_analyzer.settings["rust-analyzer"] = {
        check = {
          command = "clippy",
        },

        cargo = {
          allFeatures = true,
        },

        completion = {
          excludeTraits = {
            "core::borrow::Borrow",
            "core::borrow::BorrowMut",
            "std::borrow::Borrow",
            "std::borrow::BorrowMut",
            "owo_colors::OwoColorize",
            "color_eyre::owo_colors::OwoColorize",
          },
        },

        rustfmt = {
          extraArgs = {
            "+nightly",
          },
        },
      }

      return opts
    end,
  },
}

Edit

Later in the logs I see the following, maybe it's helpful

    client_config: (
        FullConfigInput {
            global: GlobalConfigInput,
            workspace: WorkspaceConfigInput {
                cargo_features: All,
                check_command: "clippy",
                rustfmt_extraArgs: [
                    "+nightly",
                ],
            },
            local: LocalConfigInput,
            client: ClientConfigInput,
        },
        ConfigErrors(
            [],
        ),
    ),

0xangelo avatar Jan 10 '25 13:01 0xangelo

Hmm, it does work for me in vscode

Veykril avatar Jan 10 '25 14:01 Veykril

For those who are using Neovim and may run into the same problems as I did above, getting rid of rustaceanvim did it for me. I've spent way too much time trying to get it to work with it

0xangelo avatar Jan 10 '25 16:01 0xangelo