From 8d0e3d03d537f0f69b8db916a799352ed410cefe Mon Sep 17 00:00:00 2001 From: sofia Date: Sat, 2 Aug 2025 03:41:08 +0300 Subject: [PATCH] Improve syntax highlighting --- reid-lsp/src/main.rs | 42 ++++++++++++++------- reid-lsp/syntaxes/grammar.json | 69 +++++++++++++++++++--------------- reid-lsp/syntaxes/grammar.yaml | 35 ++++++++--------- 3 files changed, 85 insertions(+), 61 deletions(-) diff --git a/reid-lsp/src/main.rs b/reid-lsp/src/main.rs index ebd45e2..ba5e5b2 100644 --- a/reid-lsp/src/main.rs +++ b/reid-lsp/src/main.rs @@ -12,9 +12,9 @@ use reid::{compile_module, parse_module, perform_all_passes}; use tower_lsp::lsp_types::{ self, CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic, DiagnosticSeverity, DidChangeTextDocumentParams, DidOpenTextDocumentParams, Hover, HoverContents, HoverParams, HoverProviderCapability, - InitializeParams, InitializeResult, InitializedParams, MarkedString, MessageType, OneOf, Range, ServerCapabilities, - TextDocumentItem, TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions, - WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities, + InitializeParams, InitializeResult, InitializedParams, MarkedString, MarkupContent, MarkupKind, MessageType, OneOf, + Range, ServerCapabilities, TextDocumentItem, TextDocumentSyncCapability, TextDocumentSyncKind, + TextDocumentSyncOptions, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities, }; use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc}; @@ -91,24 +91,38 @@ impl LanguageServer for Backend { None }; - let ty = if let Some(token) = token { + let (range, ty) = if let Some(token) = token { if let Some(possible_ty) = self.types.get(&file_name).unwrap().get(token) { + let start = token.position; + let end = token.position.add(token.token.len() as u32); + let range = Range { + start: lsp_types::Position { + line: (start.1 as i32 - 1).max(0) as u32, + character: (start.0 as i32 - 1).max(0) as u32, + }, + end: lsp_types::Position { + line: (end.1 as i32 - 1).max(0) as u32, + character: (end.0 as i32 - 1).max(0) as u32, + }, + }; if let Some(ty) = possible_ty.clone() { - format!("{}", ty) + (Some(range), format!("{}", ty)) } else { - String::from("no type") + (Some(range), String::from("no type")) } } else { - String::from("no token") + (None, String::from("no token")) } } else { - String::from("no token") + (None, String::from("no token")) }; - Ok(Some(Hover { - contents: HoverContents::Scalar(MarkedString::String(format!("{}", ty))), - range: None, - })) + let contents = HoverContents::Markup(MarkupContent { + kind: MarkupKind::Markdown, + value: format!("`{ty}`"), + }); + + Ok(Some(Hover { contents, range })) } async fn did_open(&self, params: DidOpenTextDocumentParams) { @@ -277,6 +291,8 @@ pub fn find_type_in_context(module: &mir::Module, token_idx: usize) -> Option Some(literal.as_type()), - mir::ExprKind::BinOp(binary_operator, lhs, rhs, type_kind) => { + mir::ExprKind::BinOp(_, lhs, rhs, type_kind) => { if let Some(ty) = find_type_in_expr(lhs, module_id, token_idx) { return Some(ty); } diff --git a/reid-lsp/syntaxes/grammar.json b/reid-lsp/syntaxes/grammar.json index 047a58f..5faae74 100644 --- a/reid-lsp/syntaxes/grammar.json +++ b/reid-lsp/syntaxes/grammar.json @@ -4,9 +4,6 @@ { "include": "#import" }, - { - "include": "#extern" - }, { "include": "#expression" } @@ -62,6 +59,9 @@ { "include": "#fn-signature" }, + { + "include": "#common-type" + }, { "include": "#binop-impl" }, @@ -89,15 +89,6 @@ { "include": "#parenthesis" }, - { - "include": "#number-literal" - }, - { - "include": "#string-literal" - }, - { - "include": "#common-type" - }, { "include": "#array" }, @@ -107,6 +98,12 @@ { "include": "#struct-expression" }, + { + "include": "#number-literal" + }, + { + "include": "#string-literal" + }, { "include": "#identifier" }, @@ -119,20 +116,6 @@ "match": "\\/\\/(.|\\/)*", "name": "comment.line.double-slash.reid" }, - "extern": { - "begin": "extern", - "end": "\\;", - "beginCaptures": { - "0": { - "name": "keyword.fn.reid" - } - }, - "patterns": [ - { - "include": "#expression" - } - ] - }, "fn-signature": { "begin": "(fn)\\s*(\\w+)\\(", "beginCaptures": { @@ -245,8 +228,24 @@ ] }, "number-literal": { - "match": "[0-9]+(\\.[0-9]+)?", - "name": "constant.numeric" + "patterns": [ + { + "match": "0x[0-9a-fA-F]+(\\.[0-9a-fA-F]+)?", + "name": "constant.hexadecimal" + }, + { + "match": "0o[0-7]+(\\.[0-7]+)?", + "name": "constant.octal" + }, + { + "match": "0b[01]+(\\.[01]+)?", + "name": "constant.binary" + }, + { + "match": "[0-9]+(\\.[0-9]+)?", + "name": "constant.numeric" + } + ] }, "string-literal": { "begin": "\"", @@ -338,13 +337,21 @@ ] }, "identifier": { - "match": "\\w+", - "name": "variable.language.reid" + "patterns": [ + { + "match": "[A-Z]\\w*", + "name": "entity.name.type.reid" + }, + { + "match": "\\w+", + "name": "variable.language.reid" + } + ] }, "keywords": { "patterns": [ { - "match": "let|mut|pub", + "match": "let|mut|pub|extern", "name": "storage.type.reid" }, { diff --git a/reid-lsp/syntaxes/grammar.yaml b/reid-lsp/syntaxes/grammar.yaml index 6c8c6bb..abc3621 100644 --- a/reid-lsp/syntaxes/grammar.yaml +++ b/reid-lsp/syntaxes/grammar.yaml @@ -1,7 +1,6 @@ scopeName: source.reid patterns: - include: "#import" - - include: "#extern" - include: "#expression" repository: # function-definition: @@ -47,6 +46,7 @@ repository: patterns: - include: "#comment" - include: "#fn-signature" + - include: "#common-type" - include: "#binop-impl" - include: "#type-impl" - include: "#struct-definition" @@ -56,25 +56,16 @@ repository: - include: "#cast" - include: "#function-call" - include: "#parenthesis" - - include: "#number-literal" - - include: "#string-literal" - - include: "#common-type" - include: "#array" - include: "#keywords" - include: "#struct-expression" + - include: "#number-literal" + - include: "#string-literal" - include: "#identifier" - include: "#punctuation" comment: match: "\\/\\/(.|\\/)*" name: comment.line.double-slash.reid - extern: - begin: "extern" - end: "\\;" - beginCaptures: - 0: - name: keyword.fn.reid - patterns: - - include: "#expression" fn-signature: begin: "(fn)\\s*(\\w+)\\(" beginCaptures: @@ -144,8 +135,15 @@ repository: patterns: - include: "#expression" number-literal: - match: "[0-9]+(\\.[0-9]+)?" - name: "constant.numeric" + patterns: + - match: "0x[0-9a-fA-F]+(\\.[0-9a-fA-F]+)?" + name: "constant.hexadecimal" + - match: "0o[0-7]+(\\.[0-7]+)?" + name: "constant.octal" + - match: "0b[01]+(\\.[01]+)?" + name: "constant.binary" + - match: "[0-9]+(\\.[0-9]+)?" + name: "constant.numeric" string-literal: begin: '"' end: '"' @@ -200,11 +198,14 @@ repository: patterns: - include: "#expression" identifier: - match: "\\w+" - name: variable.language.reid + patterns: + - match: "[A-Z]\\w*" + name: entity.name.type.reid + - match: "\\w+" + name: variable.language.reid keywords: patterns: - - match: "let|mut|pub" + - match: "let|mut|pub|extern" name: "storage.type.reid" - match: "if|return" name: "keyword.control"