Improve syntax highlighting

This commit is contained in:
Sofia 2025-08-02 03:41:08 +03:00
parent 34e31549b3
commit 8d0e3d03d5
3 changed files with 85 additions and 61 deletions

View File

@ -12,9 +12,9 @@ use reid::{compile_module, parse_module, perform_all_passes};
use tower_lsp::lsp_types::{ use tower_lsp::lsp_types::{
self, CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic, DiagnosticSeverity, self, CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic, DiagnosticSeverity,
DidChangeTextDocumentParams, DidOpenTextDocumentParams, Hover, HoverContents, HoverParams, HoverProviderCapability, DidChangeTextDocumentParams, DidOpenTextDocumentParams, Hover, HoverContents, HoverParams, HoverProviderCapability,
InitializeParams, InitializeResult, InitializedParams, MarkedString, MessageType, OneOf, Range, ServerCapabilities, InitializeParams, InitializeResult, InitializedParams, MarkedString, MarkupContent, MarkupKind, MessageType, OneOf,
TextDocumentItem, TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions, Range, ServerCapabilities, TextDocumentItem, TextDocumentSyncCapability, TextDocumentSyncKind,
WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities, TextDocumentSyncOptions, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
}; };
use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc}; use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc};
@ -91,24 +91,38 @@ impl LanguageServer for Backend {
None 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) { 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() { if let Some(ty) = possible_ty.clone() {
format!("{}", ty) (Some(range), format!("{}", ty))
} else { } else {
String::from("no type") (Some(range), String::from("no type"))
} }
} else { } else {
String::from("no token") (None, String::from("no token"))
} }
} else { } else {
String::from("no token") (None, String::from("no token"))
}; };
Ok(Some(Hover { let contents = HoverContents::Markup(MarkupContent {
contents: HoverContents::Scalar(MarkedString::String(format!("{}", ty))), kind: MarkupKind::Markdown,
range: None, value: format!("`{ty}`"),
})) });
Ok(Some(Hover { contents, range }))
} }
async fn did_open(&self, params: DidOpenTextDocumentParams) { async fn did_open(&self, params: DidOpenTextDocumentParams) {
@ -277,6 +291,8 @@ pub fn find_type_in_context(module: &mir::Module, token_idx: usize) -> Option<Ty
if !meta.contains(token_idx) { if !meta.contains(token_idx) {
continue; continue;
} }
} else {
continue;
} }
return match &binop.fn_kind { return match &binop.fn_kind {
@ -404,7 +420,7 @@ pub fn find_type_in_expr(expr: &mir::Expression, module_id: SourceModuleId, toke
Some(TypeKind::CustomType(mir::CustomTypeKey(name.clone(), module_id))) Some(TypeKind::CustomType(mir::CustomTypeKey(name.clone(), module_id)))
} }
mir::ExprKind::Literal(literal) => Some(literal.as_type()), mir::ExprKind::Literal(literal) => 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) { if let Some(ty) = find_type_in_expr(lhs, module_id, token_idx) {
return Some(ty); return Some(ty);
} }

View File

@ -4,9 +4,6 @@
{ {
"include": "#import" "include": "#import"
}, },
{
"include": "#extern"
},
{ {
"include": "#expression" "include": "#expression"
} }
@ -62,6 +59,9 @@
{ {
"include": "#fn-signature" "include": "#fn-signature"
}, },
{
"include": "#common-type"
},
{ {
"include": "#binop-impl" "include": "#binop-impl"
}, },
@ -89,15 +89,6 @@
{ {
"include": "#parenthesis" "include": "#parenthesis"
}, },
{
"include": "#number-literal"
},
{
"include": "#string-literal"
},
{
"include": "#common-type"
},
{ {
"include": "#array" "include": "#array"
}, },
@ -107,6 +98,12 @@
{ {
"include": "#struct-expression" "include": "#struct-expression"
}, },
{
"include": "#number-literal"
},
{
"include": "#string-literal"
},
{ {
"include": "#identifier" "include": "#identifier"
}, },
@ -119,20 +116,6 @@
"match": "\\/\\/(.|\\/)*", "match": "\\/\\/(.|\\/)*",
"name": "comment.line.double-slash.reid" "name": "comment.line.double-slash.reid"
}, },
"extern": {
"begin": "extern",
"end": "\\;",
"beginCaptures": {
"0": {
"name": "keyword.fn.reid"
}
},
"patterns": [
{
"include": "#expression"
}
]
},
"fn-signature": { "fn-signature": {
"begin": "(fn)\\s*(\\w+)\\(", "begin": "(fn)\\s*(\\w+)\\(",
"beginCaptures": { "beginCaptures": {
@ -245,8 +228,24 @@
] ]
}, },
"number-literal": { "number-literal": {
"match": "[0-9]+(\\.[0-9]+)?", "patterns": [
"name": "constant.numeric" {
"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": { "string-literal": {
"begin": "\"", "begin": "\"",
@ -338,13 +337,21 @@
] ]
}, },
"identifier": { "identifier": {
"match": "\\w+", "patterns": [
"name": "variable.language.reid" {
"match": "[A-Z]\\w*",
"name": "entity.name.type.reid"
},
{
"match": "\\w+",
"name": "variable.language.reid"
}
]
}, },
"keywords": { "keywords": {
"patterns": [ "patterns": [
{ {
"match": "let|mut|pub", "match": "let|mut|pub|extern",
"name": "storage.type.reid" "name": "storage.type.reid"
}, },
{ {

View File

@ -1,7 +1,6 @@
scopeName: source.reid scopeName: source.reid
patterns: patterns:
- include: "#import" - include: "#import"
- include: "#extern"
- include: "#expression" - include: "#expression"
repository: repository:
# function-definition: # function-definition:
@ -47,6 +46,7 @@ repository:
patterns: patterns:
- include: "#comment" - include: "#comment"
- include: "#fn-signature" - include: "#fn-signature"
- include: "#common-type"
- include: "#binop-impl" - include: "#binop-impl"
- include: "#type-impl" - include: "#type-impl"
- include: "#struct-definition" - include: "#struct-definition"
@ -56,25 +56,16 @@ repository:
- include: "#cast" - include: "#cast"
- include: "#function-call" - include: "#function-call"
- include: "#parenthesis" - include: "#parenthesis"
- include: "#number-literal"
- include: "#string-literal"
- include: "#common-type"
- include: "#array" - include: "#array"
- include: "#keywords" - include: "#keywords"
- include: "#struct-expression" - include: "#struct-expression"
- include: "#number-literal"
- include: "#string-literal"
- include: "#identifier" - include: "#identifier"
- include: "#punctuation" - include: "#punctuation"
comment: comment:
match: "\\/\\/(.|\\/)*" match: "\\/\\/(.|\\/)*"
name: comment.line.double-slash.reid name: comment.line.double-slash.reid
extern:
begin: "extern"
end: "\\;"
beginCaptures:
0:
name: keyword.fn.reid
patterns:
- include: "#expression"
fn-signature: fn-signature:
begin: "(fn)\\s*(\\w+)\\(" begin: "(fn)\\s*(\\w+)\\("
beginCaptures: beginCaptures:
@ -144,8 +135,15 @@ repository:
patterns: patterns:
- include: "#expression" - include: "#expression"
number-literal: number-literal:
match: "[0-9]+(\\.[0-9]+)?" patterns:
name: "constant.numeric" - 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: string-literal:
begin: '"' begin: '"'
end: '"' end: '"'
@ -200,11 +198,14 @@ repository:
patterns: patterns:
- include: "#expression" - include: "#expression"
identifier: identifier:
match: "\\w+" patterns:
name: variable.language.reid - match: "[A-Z]\\w*"
name: entity.name.type.reid
- match: "\\w+"
name: variable.language.reid
keywords: keywords:
patterns: patterns:
- match: "let|mut|pub" - match: "let|mut|pub|extern"
name: "storage.type.reid" name: "storage.type.reid"
- match: "if|return" - match: "if|return"
name: "keyword.control" name: "keyword.control"