Skip to content

Commit b0c3771

Browse files
committed
Support LSP features for Markdown/RST in comments
1 parent 28de3de commit b0c3771

File tree

29 files changed

+1847
-130
lines changed

29 files changed

+1847
-130
lines changed

crates/emmylua_code_analysis/resources/schema.json

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"$ref": "#/$defs/EmmyrcDoc",
5151
"default": {
5252
"knownTags": [],
53-
"privateName": []
53+
"privateName": [],
54+
"syntax": "none"
5455
}
5556
},
5657
"documentColor": {
@@ -123,7 +124,8 @@
123124
"semanticTokens": {
124125
"$ref": "#/$defs/EmmyrcSemanticToken",
125126
"default": {
126-
"enable": true
127+
"enable": true,
128+
"renderDocumentationMarkup": false
127129
}
128130
},
129131
"signature": {
@@ -442,6 +444,15 @@
442444
}
443445
]
444446
},
447+
"DocSyntax": {
448+
"type": "string",
449+
"enum": [
450+
"none",
451+
"md",
452+
"myst",
453+
"rst"
454+
]
455+
},
445456
"EmmyrcCodeAction": {
446457
"type": "object",
447458
"properties": {
@@ -611,6 +622,25 @@
611622
"items": {
612623
"type": "string"
613624
}
625+
},
626+
"rstDefaultRole": {
627+
"description": "When `syntax` is `Myst` or `Rst`, specifies default role used\nwith RST processor.",
628+
"type": [
629+
"string",
630+
"null"
631+
]
632+
},
633+
"rstPrimaryDomain": {
634+
"description": "When `syntax` is `Myst` or `Rst`, specifies primary domain used\nwith RST processor.",
635+
"type": [
636+
"string",
637+
"null"
638+
]
639+
},
640+
"syntax": {
641+
"description": "Syntax for highlighting documentation.",
642+
"$ref": "#/$defs/DocSyntax",
643+
"default": "none"
614644
}
615645
}
616646
},
@@ -934,6 +964,12 @@
934964
"type": "boolean",
935965
"default": true,
936966
"x-vscode-setting": true
967+
},
968+
"renderDocumentationMarkup": {
969+
"description": "Render Markdown/RST in documentation. Set `doc.syntax` for this option to have effect.",
970+
"type": "boolean",
971+
"default": false,
972+
"x-vscode-setting": true
937973
}
938974
}
939975
},

crates/emmylua_code_analysis/src/config/configs/doc.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,52 @@ use serde::{Deserialize, Serialize};
44
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
55
#[serde(rename_all = "camelCase")]
66
pub struct EmmyrcDoc {
7-
#[serde(default)]
87
/// Treat specific field names as private, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are private, witch can only be accessed in the class where the definition is located.
8+
#[serde(default)]
99
pub private_name: Vec<String>,
1010

1111
/// List of known documentation tags.
1212
#[serde(default)]
1313
pub known_tags: Vec<String>,
14+
15+
/// Syntax for highlighting documentation.
16+
#[serde(default)]
17+
pub syntax: DocSyntax,
18+
19+
/// When `syntax` is `Myst` or `Rst`, specifies primary domain used
20+
/// with RST processor.
21+
#[serde(skip_serializing_if = "Option::is_none")]
22+
pub rst_primary_domain: Option<String>,
23+
24+
/// When `syntax` is `Myst` or `Rst`, specifies default role used
25+
/// with RST processor.
26+
#[serde(skip_serializing_if = "Option::is_none")]
27+
pub rst_default_role: Option<String>,
1428
}
1529

1630
impl Default for EmmyrcDoc {
1731
fn default() -> Self {
1832
Self {
1933
private_name: Default::default(),
2034
known_tags: Default::default(),
35+
syntax: Default::default(),
36+
rst_primary_domain: None,
37+
rst_default_role: None,
2138
}
2239
}
2340
}
41+
42+
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
43+
#[serde(rename_all = "kebab-case")]
44+
pub enum DocSyntax {
45+
None,
46+
Md,
47+
Myst,
48+
Rst,
49+
}
50+
51+
impl Default for DocSyntax {
52+
fn default() -> Self {
53+
DocSyntax::None
54+
}
55+
}

crates/emmylua_code_analysis/src/config/configs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use code_action::EmmyrcCodeAction;
2020
pub use codelen::EmmyrcCodeLens;
2121
pub use completion::{EmmyrcCompletion, EmmyrcFilenameConvention};
2222
pub use diagnostics::EmmyrcDiagnostic;
23-
pub use doc::EmmyrcDoc;
23+
pub use doc::{DocSyntax, EmmyrcDoc};
2424
pub use document_color::EmmyrcDocumentColor;
2525
pub use hover::EmmyrcHover;
2626
pub use inlayhint::EmmyrcInlayHint;

crates/emmylua_code_analysis/src/config/configs/semantictoken.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ pub struct EmmyrcSemanticToken {
88
#[serde(default = "default_true")]
99
#[schemars(extend("x-vscode-setting" = true))]
1010
pub enable: bool,
11+
12+
/// Render Markdown/RST in documentation. Set `doc.syntax` for this option to have effect.
13+
#[serde(default)]
14+
#[schemars(extend("x-vscode-setting" = true))]
15+
pub render_documentation_markup: bool,
1116
}
1217

1318
impl Default for EmmyrcSemanticToken {
1419
fn default() -> Self {
1520
Self {
1621
enable: default_true(),
22+
render_documentation_markup: false,
1723
}
1824
}
1925
}

crates/emmylua_code_analysis/src/config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use std::{
99

1010
pub use crate::config::configs::{EmmyrcExternalTool, EmmyrcReformat};
1111
pub use config_loader::{load_configs, load_configs_raw};
12+
pub use configs::{DocSyntax, EmmyrcFilenameConvention, EmmyrcLuaVersion};
1213
use configs::{
1314
EmmyrcCodeAction, EmmyrcCodeLens, EmmyrcCompletion, EmmyrcDiagnostic, EmmyrcDoc,
1415
EmmyrcDocumentColor, EmmyrcHover, EmmyrcInlayHint, EmmyrcInlineValues, EmmyrcReference,
1516
EmmyrcResource, EmmyrcRuntime, EmmyrcSemanticToken, EmmyrcSignature, EmmyrcStrict,
1617
EmmyrcWorkspace,
1718
};
18-
pub use configs::{EmmyrcFilenameConvention, EmmyrcLuaVersion};
1919
use emmylua_parser::{LuaLanguageLevel, LuaNonStdSymbolSet, ParserConfig, SpecialFunction};
2020
use regex::Regex;
2121
use rowan::NodeCache;

crates/emmylua_code_analysis/src/semantic/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use visibility::check_export_visibility;
4040
use visibility::check_visibility;
4141

4242
use crate::semantic::member::find_members_with_key;
43-
use crate::{Emmyrc, LuaDocument, LuaSemanticDeclId, db_index::LuaTypeDeclId};
43+
use crate::{Emmyrc, LuaDocument, LuaSemanticDeclId, ModuleInfo, db_index::LuaTypeDeclId};
4444
use crate::{
4545
FileId,
4646
db_index::{DbIndex, LuaType},
@@ -87,6 +87,10 @@ impl<'a> SemanticModel<'a> {
8787
self.db.get_vfs().get_document(&self.file_id).unwrap()
8888
}
8989

90+
pub fn get_module(&self) -> Option<&ModuleInfo> {
91+
self.db.get_module_index().get_module(self.file_id)
92+
}
93+
9094
pub fn get_document_by_file_id(&self, file_id: FileId) -> Option<LuaDocument> {
9195
self.db.get_vfs().get_document(&file_id)
9296
}
@@ -256,6 +260,10 @@ impl<'a> SemanticModel<'a> {
256260
&self.emmyrc
257261
}
258262

263+
pub fn get_emmyrc_arc(&self) -> Arc<Emmyrc> {
264+
self.emmyrc.clone()
265+
}
266+
259267
pub fn get_root(&self) -> &LuaChunk {
260268
&self.root
261269
}

crates/emmylua_ls/src/handlers/completion/completion_builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::HashSet;
33
use emmylua_code_analysis::SemanticModel;
44
use emmylua_parser::LuaSyntaxToken;
55
use lsp_types::{CompletionItem, CompletionTriggerKind};
6+
use rowan::TextSize;
67
use tokio_util::sync::CancellationToken;
78

89
pub struct CompletionBuilder<'a> {
@@ -15,6 +16,7 @@ pub struct CompletionBuilder<'a> {
1516
pub trigger_kind: CompletionTriggerKind,
1617
/// 是否为空格字符触发的补全(非主动触发)
1718
pub is_space_trigger_character: bool,
19+
pub position_offset: TextSize,
1820
}
1921

2022
impl<'a> CompletionBuilder<'a> {
@@ -23,6 +25,7 @@ impl<'a> CompletionBuilder<'a> {
2325
semantic_model: SemanticModel<'a>,
2426
cancel_token: CancellationToken,
2527
trigger_kind: CompletionTriggerKind,
28+
position_offset: TextSize,
2629
) -> Self {
2730
let is_space_trigger_character = if trigger_kind == CompletionTriggerKind::TRIGGER_CHARACTER
2831
{
@@ -40,6 +43,7 @@ impl<'a> CompletionBuilder<'a> {
4043
stopped: false,
4144
trigger_kind,
4245
is_space_trigger_character,
46+
position_offset,
4347
}
4448
}
4549

crates/emmylua_ls/src/handlers/completion/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ pub fn completion(
8080
}
8181
};
8282

83-
let mut builder = CompletionBuilder::new(token, semantic_model, cancel_token, trigger_kind);
84-
let emmyrc = analysis.get_emmyrc();
85-
add_completions(&mut builder, &emmyrc);
83+
let mut builder = CompletionBuilder::new(
84+
token,
85+
semantic_model,
86+
cancel_token,
87+
trigger_kind,
88+
position_offset,
89+
);
90+
add_completions(&mut builder);
8691
Some(CompletionResponse::Array(builder.get_completion_items()))
8792
}
8893

0 commit comments

Comments
 (0)