Unverified Commit 7f36c78e authored by Austin Horstman's avatar Austin Horstman Committed by GitHub
Browse files

rust-analyzer-unwrapped: 2024-09-02 -> 2024-11-11 (#354304)

parents 3660fd73 66689ef5
Loading
Loading
Loading
Loading
+32 −19
Original line number Diff line number Diff line
{ lib
, stdenv
, callPackage
, fetchFromGitHub
, rustPlatform
, CoreServices
, cmake
, libiconv
, useMimalloc ? false
, doCheck ? true
, nix-update-script
{
  lib,
  stdenv,
  fetchFromGitHub,
  rustPlatform,
  cmake,
  libiconv,
  useMimalloc ? false,
  doCheck ? true,
  nix-update-script,
}:

rustPlatform.buildRustPackage rec {
  pname = "rust-analyzer-unwrapped";
  version = "2024-09-02";
  cargoHash = "sha256-t45RzYkuywGByGWwUON3dW0aKjLYcFXB8uy4CybPuf4=";
  version = "2024-11-11";
  cargoHash = "sha256-lzbk/APerZih7+1ZxBKl0rUHCJJA8W8RIqalEfu+MFI=";

  src = fetchFromGitHub {
    owner = "rust-lang";
    repo = "rust-analyzer";
    rev = version;
    hash = "sha256-YH0kH5CSOnAuPUB1BUzUqvnKiv5SgDhfMNjrkki9Ahk=";
    hash = "sha256-TDI1s2Sc/rpMLwful1NcTjYBbqzbQ4Mjw5PPOuOXVfg=";
  };

  cargoBuildFlags = [ "--bin" "rust-analyzer" "--bin" "rust-analyzer-proc-macro-srv" ];
  cargoTestFlags = [ "--package" "rust-analyzer" "--package" "proc-macro-srv-cli" ];
  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.
@@ -33,7 +42,6 @@ rustPlatform.buildRustPackage rec {
  nativeBuildInputs = lib.optional useMimalloc cmake;

  buildInputs = lib.optionals stdenv.hostPlatform.isDarwin [
    CoreServices
    libiconv
  ];

@@ -58,13 +66,18 @@ rustPlatform.buildRustPackage rec {
  passthru = {
    updateScript = nix-update-script { };
    # FIXME: Pass overrided `rust-analyzer` once `buildRustPackage` also implements #119942
    tests.neovim-lsp = callPackage ./test-neovim-lsp.nix { };
    # FIXME: test script can't find rust std lib so hover doesn't return expected result
    # https://github.com/NixOS/nixpkgs/pull/354304
    # tests.neovim-lsp = callPackage ./test-neovim-lsp.nix { };
  };

  meta = with lib; {
    description = "Modular compiler frontend for the Rust language";
    homepage = "https://rust-analyzer.github.io";
    license = with licenses; [ mit asl20 ];
    license = with licenses; [
      mit
      asl20
    ];
    maintainers = with maintainers; [ oxalica ];
    mainProgram = "rust-analyzer";
  };
+75 −0
Original line number Diff line number Diff line
{
  runCommand,
  cargo,
  neovim,
  rust-analyzer,
  rustc,
}:
runCommand "test-neovim-rust-analyzer"
  {
    nativeBuildInputs = [
      cargo
      neovim
      rust-analyzer
      rustc
    ];

    testRustSrc = ''
      fn main() {
        let mut var = vec![None];
        var.push(Some("hello".to_owned()));
      }
    '';

    # NB. Wait for server done type calculations before sending `hover` request,
    # otherwise it would return `{unknown}`.
    # Ref: https://github.com/rust-lang/rust-analyzer/blob/7b11fdeb681c12002861b9804a388efde81c9647/docs/dev/lsp-extensions.md#server-status
    nvimConfig = ''
      local caps = vim.lsp.protocol.make_client_capabilities()
      caps["experimental"] = { serverStatusNotification = true }
      vim.lsp.buf_attach_client(vim.api.nvim_get_current_buf(), vim.lsp.start_client({
        cmd = { "rust-analyzer" },
        capabilities = caps,
        handlers = {
          ["experimental/serverStatus"] = function(_, msg, ctx)
            if msg.health == "ok" then
              if msg.quiescent then
                vim.cmd("goto 23") -- let mut |var =...
                vim.lsp.buf.hover()
              end
            else
              print("error: server status is not ok: ")
              vim.cmd("q")
            end
          end,
          ["textDocument/hover"] = function(_, msg, ctx)
            if msg then
              -- Keep newlines.
              io.write(msg.contents.value)
              vim.cmd("q")
            end
          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
  ''
+21 −0
Original line number Diff line number Diff line
{
  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}"
  ''
+0 −62
Original line number Diff line number Diff line
{ 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()));
    }
  '';

  # NB. Wait for server done type calculations before sending `hover` request,
  # otherwise it would return `{unknown}`.
  # Ref: https://github.com/rust-lang/rust-analyzer/blob/7b11fdeb681c12002861b9804a388efde81c9647/docs/dev/lsp-extensions.md#server-status
  nvimConfig = /* lua */ ''
    local caps = vim.lsp.protocol.make_client_capabilities()
    caps["experimental"] = { serverStatusNotification = true }
    vim.lsp.buf_attach_client(vim.api.nvim_get_current_buf(), vim.lsp.start_client({
      cmd = { "rust-analyzer" },
      capabilities = caps,
      handlers = {
        ["experimental/serverStatus"] = function(_, msg, ctx)
          if msg.health == "ok" then
            if msg.quiescent then
              vim.cmd("goto 23") -- let mut |var =...
              vim.lsp.buf.hover()
            end
          else
            print("error: server status is not ok: ")
            vim.cmd("q")
          end
        end,
        ["textDocument/hover"] = function(_, msg, ctx)
          if msg then
            -- Keep newlines.
            io.write(msg.contents.value)
            vim.cmd("q")
          end
        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
''
+0 −15
Original line number Diff line number Diff line
{ 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}"
''
Loading