about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/tools/rust/rust-analyzer
diff options
context:
space:
mode:
Diffstat (limited to 'nixpkgs/pkgs/development/tools/rust/rust-analyzer')
-rw-r--r--nixpkgs/pkgs/development/tools/rust/rust-analyzer/default.nix71
-rw-r--r--nixpkgs/pkgs/development/tools/rust/rust-analyzer/test-neovim-lsp.nix49
-rw-r--r--nixpkgs/pkgs/development/tools/rust/rust-analyzer/wrapper.nix15
3 files changed, 135 insertions, 0 deletions
diff --git a/nixpkgs/pkgs/development/tools/rust/rust-analyzer/default.nix b/nixpkgs/pkgs/development/tools/rust/rust-analyzer/default.nix
new file mode 100644
index 000000000000..04bb998ab551
--- /dev/null
+++ b/nixpkgs/pkgs/development/tools/rust/rust-analyzer/default.nix
@@ -0,0 +1,71 @@
+{ lib
+, stdenv
+, callPackage
+, fetchFromGitHub
+, rustPlatform
+, CoreServices
+, cmake
+, libiconv
+, useMimalloc ? false
+, doCheck ? true
+, nix-update-script
+}:
+
+rustPlatform.buildRustPackage rec {
+  pname = "rust-analyzer-unwrapped";
+  version = "2024-03-18";
+  cargoSha256 = "sha256-CZC90HtAuK66zXDCHam9YJet9C62psxkHeJ/+1vIjTg=";
+
+  src = fetchFromGitHub {
+    owner = "rust-lang";
+    repo = "rust-analyzer";
+    rev = version;
+    sha256 = "sha256-Jd6pmXlwKk5uYcjyO/8BfbUVmx8g31Qfk7auI2IG66A=";
+  };
+
+  cargoBuildFlags = [ "--bin" "rust-analyzer" "--bin" "rust-analyzer-proc-macro-srv" ];
+  cargoTestFlags = [ "--package" "rust-analyzer" "--package" "proc-macro-srv-cli" ];
+
+  # Code format check requires more dependencies but don't really matter for packaging.
+  # So just ignore it.
+  checkFlags = [ "--skip=tidy::check_code_formatting" ];
+
+  nativeBuildInputs = lib.optional useMimalloc cmake;
+
+  buildInputs = lib.optionals stdenv.isDarwin [
+    CoreServices
+    libiconv
+  ];
+
+  buildFeatures = lib.optional useMimalloc "mimalloc";
+
+  CFG_RELEASE = version;
+
+  inherit doCheck;
+  preCheck = lib.optionalString doCheck ''
+    export RUST_SRC_PATH=${rustPlatform.rustLibSrc}
+  '';
+
+  doInstallCheck = true;
+  installCheckPhase = ''
+    runHook preInstallCheck
+    versionOutput="$($out/bin/rust-analyzer --version)"
+    echo "'rust-analyzer --version' returns: $versionOutput"
+    [[ "$versionOutput" == "rust-analyzer ${version}" ]]
+    runHook postInstallCheck
+  '';
+
+  passthru = {
+    updateScript = nix-update-script { };
+    # FIXME: Pass overrided `rust-analyzer` once `buildRustPackage` also implements #119942
+    tests.neovim-lsp = callPackage ./test-neovim-lsp.nix { };
+  };
+
+  meta = with lib; {
+    description = "A modular compiler frontend for the Rust language";
+    homepage = "https://rust-analyzer.github.io";
+    license = with licenses; [ mit asl20 ];
+    maintainers = with maintainers; [ oxalica ];
+    mainProgram = "rust-analyzer";
+  };
+}
diff --git a/nixpkgs/pkgs/development/tools/rust/rust-analyzer/test-neovim-lsp.nix b/nixpkgs/pkgs/development/tools/rust/rust-analyzer/test-neovim-lsp.nix
new file mode 100644
index 000000000000..963e134075ed
--- /dev/null
+++ b/nixpkgs/pkgs/development/tools/rust/rust-analyzer/test-neovim-lsp.nix
@@ -0,0 +1,49 @@
+{ runCommand, cargo, neovim, rust-analyzer, rustc }:
+runCommand "test-neovim-rust-analyzer" {
+  nativeBuildInputs = [ cargo neovim rust-analyzer rustc ];
+
+  testRustSrc = /* rust */ ''
+    fn main() {
+      let mut var = vec![None];
+      var.push(Some("hello".to_owned()));
+    }
+  '';
+
+  nvimConfig = /* lua */ ''
+    vim.lsp.buf_attach_client(vim.api.nvim_get_current_buf(), vim.lsp.start_client({
+      cmd = { "rust-analyzer" },
+      handlers = {
+        ["$/progress"] = function(_, msg, ctx)
+          if msg.token == "rustAnalyzer/Indexing" and msg.value.kind == "end" then
+            vim.cmd("goto 23") -- let mut |var =...
+            vim.lsp.buf.hover()
+          end
+        end,
+        ["textDocument/hover"] = function(_, msg, ctx)
+          -- Keep newlines.
+          io.write(msg.contents.value)
+          vim.cmd("q")
+        end,
+      },
+      on_error = function(code)
+        print("error: " .. code)
+        vim.cmd("q")
+      end
+    }))
+  '';
+
+} ''
+  # neovim requires a writable HOME.
+  export HOME="$(pwd)"
+
+  cargo new --bin test-rust-analyzer
+  cd test-rust-analyzer
+  cat <<<"$testRustSrc" >src/main.rs
+  cat <<<"$nvimConfig" >script.lua
+
+  # `-u` doesn't work
+  result="$(nvim --headless +'lua dofile("script.lua")' src/main.rs)"
+  echo "$result"
+  [[ "$result" == *"var: Vec<Option<String>>"* ]]
+  touch $out
+''
diff --git a/nixpkgs/pkgs/development/tools/rust/rust-analyzer/wrapper.nix b/nixpkgs/pkgs/development/tools/rust/rust-analyzer/wrapper.nix
new file mode 100644
index 000000000000..6fa5207de6eb
--- /dev/null
+++ b/nixpkgs/pkgs/development/tools/rust/rust-analyzer/wrapper.nix
@@ -0,0 +1,15 @@
+{ lib, rustPlatform, runCommand, makeWrapper, rust-analyzer-unwrapped
+, pname ? "rust-analyzer"
+, version ? rust-analyzer-unwrapped.version
+  # Use name from `RUST_SRC_PATH`
+, rustSrc ? rustPlatform.rustLibSrc
+}:
+runCommand "${pname}-${version}" {
+  inherit pname version;
+  inherit (rust-analyzer-unwrapped) src meta;
+  nativeBuildInputs = [ makeWrapper ];
+} ''
+  mkdir -p $out/bin
+  makeWrapper ${rust-analyzer-unwrapped}/bin/rust-analyzer $out/bin/rust-analyzer \
+    --set-default RUST_SRC_PATH "${rustSrc}"
+''