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::{
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<Ty
if !meta.contains(token_idx) {
continue;
}
} else {
continue;
}
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)))
}
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) {
return Some(ty);
}

View File

@ -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": {
"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": {
"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"
},
{

View File

@ -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,7 +135,14 @@ repository:
patterns:
- include: "#expression"
number-literal:
match: "[0-9]+(\\.[0-9]+)?"
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: '"'
@ -200,11 +198,14 @@ repository:
patterns:
- include: "#expression"
identifier:
match: "\\w+"
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"