Skip to content

fix: Prevent panic in semantic highlighting for unknown token types #7189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions sway-lsp/src/capabilities/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ pub fn semantic_tokens(tokens_sorted: &[&RefMulti<TokenIdent, Token>]) -> Semant
for entry in tokens_sorted {
let (ident, token) = entry.pair();
let ty = semantic_token_type(&token.kind);
let token_index = type_index(&ty);
// TODO - improve with modifiers
let modifier_bitset = 0;
builder.push(ident.range, token_index, modifier_bitset);
if let Some(token_index) = type_index(&ty) {
// TODO - improve with modifiers
let modifier_bitset = 0;
builder.push(ident.range, token_index, modifier_bitset);
} else {
tracing::error!("Unsupported token type: {:?} for token: {:#?}", ty, token);
}
}
builder.build()
}
Expand Down Expand Up @@ -151,6 +154,7 @@ pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
SemanticTokenType::new("selfKeyword"),
SemanticTokenType::new("selfTypeKeyword"),
SemanticTokenType::new("typeAlias"),
SemanticTokenType::new("traitType"),
];

pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
Expand Down Expand Up @@ -194,6 +198,9 @@ fn semantic_token_type(kind: &SymbolKind) -> SemanticTokenType {
}
}

fn type_index(ty: &SemanticTokenType) -> u32 {
SUPPORTED_TYPES.iter().position(|it| it == ty).unwrap() as u32
fn type_index(ty: &SemanticTokenType) -> Option<u32> {
SUPPORTED_TYPES
.iter()
.position(|it| it == ty)
.map(|x| x as u32)
}
24 changes: 24 additions & 0 deletions sway-lsp/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,30 @@ fn did_open() {
});
}

#[test]
fn did_open_all_std_lib_files() {
run_async!({
let (mut service, _) = LspService::new(ServerState::new);
let files = sway_utils::helpers::get_sway_files(std_lib_dir().join("src"));
for file in files {
eprintln!("opening file: {:?}", file.as_path());

// If the workspace is not initialized, we need to initialize it
// Otherwise, we can just open the file
let uri = if service.inner().sync_workspace.get().is_none() {
init_and_open(&mut service, file.to_path_buf()).await
} else {
open(service.inner(), file.to_path_buf()).await
};

// Make sure that semantic tokens are successfully returned for the file
let semantic_tokens = lsp::get_semantic_tokens_full(service.inner(), &uri).await;
assert!(!semantic_tokens.data.is_empty());
}
shutdown_and_exit(&mut service).await;
});
}

// Opens all members in the examples workspace and assert that we are able to return semantic tokens for each workspace member.
// This test is expected to run for a while although should be much faster once https://github.com/FuelLabs/sway/pull/7139 is merged.
#[test]
Expand Down
4 changes: 4 additions & 0 deletions sway-lsp/tests/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ pub fn e2e_test_dir() -> PathBuf {
.join("struct_field_access")
}

pub fn std_lib_dir() -> PathBuf {
sway_workspace_dir().join("sway-lib-std")
}

pub fn runnables_test_dir() -> PathBuf {
test_fixtures_dir().join("runnables")
}
Expand Down
Loading