Skip to content

Latest commit

Β 

History

History
62 lines (40 loc) Β· 3.16 KB

File metadata and controls

62 lines (40 loc) Β· 3.16 KB

Editors & LSP

leetcode-cli generates flat solution files (e.g. code/1.two-sum.rs). That keeps submissions simple, but a language server like rust-analyzer expects a project it recognizes. Getting full autocomplete, diagnostics, and go-to-definition is an editor-side setup, not something leetcode-cli does for you β€” and there are two clean ways to do it.

This page uses Rust as the running example; the same ideas apply to other languages and their servers.

Option A β€” single-file mode (recommended)

rust-analyzer supports detached / single files that don't belong to a Cargo workspace. This needs no extra files and no change to how leetcode-cli generates code β€” just tell your editor to treat the directory as detached.

  • VS Code β€” open the code/ directory; rust-analyzer picks up detached files automatically. You can pin them with rust-analyzer.linkedProjects if needed.
  • Neovim / other LSP clients β€” point the rust-analyzer root at the code/ directory (e.g. set the workspace root to the folder rather than searching for a Cargo.toml).
  • Helix / Zed β€” make sure rust-analyzer is launched with the code/ directory as its root; both honor rust-analyzer's detached-file handling.

If your standard library completions don't show up, confirm rustc and rust-analyzer are recent β€” detached-file support has improved over time.

Option B β€” a Cargo workspace

If you'd rather have a real crate (so cargo check/cargo test work too), put a workspace around the generated files and include! them. leetcode-cli's existing config does all the wiring β€” no patches required.

  1. Create a workspace and point leetcode-cli's storage at it:

    cargo new --lib leetcode-solutions
    # ~/.leetcode/leetcode.toml
    [storage]
    root = '/path/to/leetcode-solutions'
    code = 'solutions'

    Now leetcode edit 1 writes into leetcode-solutions/solutions/, inside a crate rust-analyzer understands.

  2. Pull the solution files into the crate from src/lib.rs:

    #![allow(dead_code, unused_imports, unused_variables, non_snake_case)]
    
    mod solutions {
        include!("../solutions/1.two-sum.rs");
        include!("../solutions/2.add-two-numbers.rs");
    }

You get full rust-analyzer features across every file, and leetcode test/leetcode exec still extract and submit the solution snippet unchanged.

Tip: one directory per problem

The [code] pick template can contain a /, so you can give each problem its own folder if your editor prefers that layout:

[code]
pick = '${fid}.${slug}/solution'

This writes code/1.two-sum/solution.rs, etc.

Why leetcode-cli doesn't generate per-problem crates

Scaffolding a Cargo crate (or any per-language project structure) for every problem would bake an editor concern into the tool, with a config flag and a directory layout to maintain forever. The two approaches above solve the same problem entirely from the editor and existing config β€” so that's where it belongs. See the discussion in #204.