Skip to content

fix: add context-aware SPDX and pragma completions#224

Open
rifuki wants to merge 2 commits into
asyncswap:mainfrom
rifuki:fix/source-unit-directive-completions
Open

fix: add context-aware SPDX and pragma completions#224
rifuki wants to merge 2 commits into
asyncswap:mainfrom
rifuki:fix/source-unit-directive-completions

Conversation

@rifuki

@rifuki rifuki commented Jun 7, 2026

Copy link
Copy Markdown

Closes #223

Problem

Completion inside source-unit directive contexts can fall through to normal/global Solidity completions. This makes early SPDX and pragma typing noisy, and SPDX insertion can duplicate the existing // prefix depending on the client.

Fix

  • Detect SPDX directive prefixes such as // S, // SP, // SPD, and // SPDX.
  • Offer full SPDX license directive lines using a line replacement edit.
  • Add pragma completions for pragma , pragma solidity , and pragma abicoder contexts.
  • Suppress unrelated generic/global completions in those directive contexts.

Tests

  • rustfmt src/completion.rs tests/completion.rs
  • cargo check
  • cargo test --test completion -- --nocapture
  • cargo test --lib -- --nocapture
  • cargo test
  • cargo build --release --bin solidity-language-server
  • git diff --check

Comment thread src/completion.rs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks like you need to specify the start and end to replace if we need the version completions.

pragma solidity 0.*| ^0.8.35;
//                 ^ completion cursor

@rifuki rifuki Jun 8, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 1e2f3e0. Version completions now use a text edit from the start of the pragma solidity value through the end of the existing version expression before ;. So the example pragma solidity 0.*| ^0.8.35; is replaced as a whole value and becomes pragma solidity ^0.8.35;, instead of inserting at the cursor or duplicating the trailing text.

Added test_context_completion_pragma_solidity_version_replaces_partial_token for this exact shape. I also tightened the pragma directive matching so pragma solidityx / pragma abicoderx do not get treated as directive contexts.

Verified with:

cargo check
cargo test --test completion test_context_completion
cargo test --test completion
cargo test

@rifuki rifuki force-pushed the fix/source-unit-directive-completions branch from 919c1ff to 6a4663e Compare June 8, 2026 16:54
@rifuki rifuki force-pushed the fix/source-unit-directive-completions branch from 6a4663e to 1e2f3e0 Compare June 27, 2026 00:38
@rifuki

rifuki commented Jun 27, 2026

Copy link
Copy Markdown
Author

Hi @mmsaki, quick follow-up on this one. I updated the PR based on your replacement-range feedback. Latest head is 1e2f3e0:

  • pragma solidity 0.*| ^0.8.35; now replaces the whole existing version value before ;, so accepting ^0.8.35 yields pragma solidity ^0.8.35; instead of duplicating trailing text.
  • Tightened directive matching so pragma solidityx / pragma abicoderx are not treated as directive contexts.

Re-ran:

cargo check
cargo test --test completion test_context_completion
cargo test --test completion
cargo test

Does this direction still look good for completion-side context handling, or would you prefer this split differently with codeActions?

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.

Completion leaks generic/global items in source-unit directive contexts

2 participants