Skip to content

Commit

Permalink
feat(lsp): generate semantic tokens for schema files
Browse files Browse the repository at this point in the history
Provide semantic tokens for schema files to provide better code
highlighting. Currently, not all possible tokens are supplied yet.
  • Loading branch information
dnaka91 committed Dec 13, 2023
1 parent edc7498 commit 57b59ee
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 86 deletions.
14 changes: 7 additions & 7 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};
use stef_parser::{
Comment, Const, DataType, Definition, Enum, ExternalType, Fields, Generics, Import, Literal,
Module, NamedField, Schema, Struct, Type, TypeAlias, UnnamedField, Variant,
LiteralValue, Module, NamedField, Schema, Struct, Type, TypeAlias, UnnamedField, Variant,
};

use super::{decode, encode, size};
Expand Down Expand Up @@ -387,11 +387,11 @@ fn compile_const_data_type(ty: &Type<'_>) -> TokenStream {
}

fn compile_literal(literal: &Literal) -> TokenStream {
match literal {
Literal::Bool(b) => quote! { #b },
Literal::Int(i) => proc_macro2::Literal::i128_unsuffixed(*i).into_token_stream(),
Literal::Float(f) => proc_macro2::Literal::f64_unsuffixed(*f).into_token_stream(),
Literal::String(s) => proc_macro2::Literal::string(s).into_token_stream(),
Literal::Bytes(b) => proc_macro2::Literal::byte_string(b).into_token_stream(),
match &literal.value {
LiteralValue::Bool(b) => quote! { #b },
LiteralValue::Int(i) => proc_macro2::Literal::i128_unsuffixed(*i).into_token_stream(),
LiteralValue::Float(f) => proc_macro2::Literal::f64_unsuffixed(*f).into_token_stream(),
LiteralValue::String(s) => proc_macro2::Literal::string(s).into_token_stream(),
LiteralValue::Bytes(b) => proc_macro2::Literal::byte_string(b).into_token_stream(),
}
}
16 changes: 8 additions & 8 deletions crates/stef-go/src/definition.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt::{self, Display, Write};

use stef_parser::{
Comment, Const, DataType, Definition, Enum, ExternalType, Fields, Generics, Literal, Name,
Schema, Struct, Type, TypeAlias, Variant,
Comment, Const, DataType, Definition, Enum, ExternalType, Fields, Generics, Literal,
LiteralValue, Name, Schema, Struct, Type, TypeAlias, Variant,
};

use crate::{decode, encode, size, Opts, Output};
Expand Down Expand Up @@ -597,12 +597,12 @@ struct RenderLiteral<'a>(&'a Literal);

impl Display for RenderLiteral<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
Literal::Bool(b) => write!(f, "{b}"),
Literal::Int(i) => write!(f, "{i}"),
Literal::Float(f2) => write!(f, "{f2}"),
Literal::String(s) => write!(f, "{s:?}"),
Literal::Bytes(b) => {
match &self.0.value {
LiteralValue::Bool(b) => write!(f, "{b}"),
LiteralValue::Int(i) => write!(f, "{i}"),
LiteralValue::Float(f2) => write!(f, "{f2}"),
LiteralValue::String(s) => write!(f, "{s:?}"),
LiteralValue::Bytes(b) => {
if b.is_empty() {
return Ok(());
}
Expand Down
64 changes: 21 additions & 43 deletions crates/stef-lsp/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use log::{as_debug, as_display, debug, error, warn};
use lsp_types::{
DidChangeConfigurationParams, DidChangeTextDocumentParams, DidCloseTextDocumentParams,
DidOpenTextDocumentParams, InitializeParams, InitializeResult, InitializedParams,
PositionEncodingKind, Registration, SemanticTokenModifier, SemanticTokenType,
SemanticTokensFullOptions, SemanticTokensLegend, SemanticTokensOptions, SemanticTokensParams,
SemanticTokensResult, SemanticTokensServerCapabilities, ServerCapabilities, ServerInfo,
TextDocumentSyncCapability, TextDocumentSyncKind, WorkDoneProgressOptions,
PositionEncodingKind, Registration, SemanticTokens, SemanticTokensFullOptions,
SemanticTokensLegend, SemanticTokensOptions, SemanticTokensParams, SemanticTokensResult,
SemanticTokensServerCapabilities, ServerCapabilities, ServerInfo, TextDocumentSyncCapability,
TextDocumentSyncKind, WorkDoneProgressOptions,
};
use ropey::Rope;

use crate::{compile, state::FileBuilder, GlobalState};
use crate::{compile, semantic_tokens, state::FileBuilder, GlobalState};

pub fn initialize(
_state: &mut GlobalState<'_>,
Expand All @@ -35,43 +35,8 @@ pub fn initialize(
work_done_progress: Some(false),
},
legend: SemanticTokensLegend {
token_types: vec![
SemanticTokenType::NAMESPACE,
SemanticTokenType::TYPE,
SemanticTokenType::CLASS,
SemanticTokenType::ENUM,
SemanticTokenType::INTERFACE,
SemanticTokenType::STRUCT,
SemanticTokenType::TYPE_PARAMETER,
SemanticTokenType::PARAMETER,
SemanticTokenType::VARIABLE,
SemanticTokenType::PROPERTY,
SemanticTokenType::ENUM_MEMBER,
SemanticTokenType::EVENT,
SemanticTokenType::FUNCTION,
SemanticTokenType::METHOD,
SemanticTokenType::MACRO,
SemanticTokenType::KEYWORD,
SemanticTokenType::MODIFIER,
SemanticTokenType::COMMENT,
SemanticTokenType::STRING,
SemanticTokenType::NUMBER,
SemanticTokenType::REGEXP,
SemanticTokenType::OPERATOR,
SemanticTokenType::DECORATOR,
],
token_modifiers: vec![
SemanticTokenModifier::DECLARATION,
SemanticTokenModifier::DEFINITION,
SemanticTokenModifier::READONLY,
SemanticTokenModifier::STATIC,
SemanticTokenModifier::DEPRECATED,
SemanticTokenModifier::ABSTRACT,
SemanticTokenModifier::ASYNC,
SemanticTokenModifier::MODIFICATION,
SemanticTokenModifier::DOCUMENTATION,
SemanticTokenModifier::DEFAULT_LIBRARY,
],
token_types: semantic_tokens::TOKEN_TYPES.to_vec(),
token_modifiers: semantic_tokens::TOKEN_MODIFIERS.to_vec(),
},
range: Some(false),
full: Some(SemanticTokensFullOptions::Bool(true)),
Expand Down Expand Up @@ -204,10 +169,23 @@ pub fn did_close(state: &mut GlobalState<'_>, params: DidCloseTextDocumentParams
}

pub fn semantic_tokens_full(
_state: &mut GlobalState<'_>,
state: &mut GlobalState<'_>,
params: SemanticTokensParams,
) -> Result<Option<SemanticTokensResult>> {
debug!(uri = as_display!(params.text_document.uri); "requested semantic tokens");

if let Some((schema, index)) = state.files.get(&params.text_document.uri).and_then(|file| {
file.borrow_schema()
.as_ref()
.ok()
.zip(Some(file.borrow_index()))
}) {
return Ok(Some(SemanticTokensResult::Tokens(SemanticTokens {
result_id: None,
data: semantic_tokens::Visitor::new(index).visit_schema(schema)?,
})));
}

Ok(None)
}

Expand Down
1 change: 1 addition & 0 deletions crates/stef-lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod compile;
mod config;
mod handlers;
mod logging;
mod semantic_tokens;
mod state;

fn main() -> Result<()> {
Expand Down
Loading

0 comments on commit 57b59ee

Please sign in to comment.