Skip to content

Commit 6b3a421

Browse files
authored
feat: support plaintext-only client (#10)
1 parent 66ea942 commit 6b3a421

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

fluent-bit-language-server/src/completion.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use flb_schema::section::FlbSectionType;
66
#[allow(unused_imports)]
77
use once_cell::sync::Lazy;
88
use tower_lsp::lsp_types::{
9-
CompletionItem, CompletionItemKind, CompletionItemLabelDetails, Documentation,
10-
InsertTextFormat, InsertTextMode, MarkupContent, MarkupKind,
9+
CompletionItem, CompletionItemKind, CompletionItemLabelDetails, Documentation, Hover,
10+
HoverContents, InsertTextFormat, InsertTextMode, MarkupContent, MarkupKind,
1111
};
1212

1313
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -16,16 +16,19 @@ pub(crate) struct FlbConfigParameterInfo {
1616
pub(crate) description: String,
1717
}
1818

19-
impl From<FlbConfigParameterInfo> for MarkupContent {
20-
fn from(info: FlbConfigParameterInfo) -> Self {
21-
let mut value = info.description.clone();
22-
if let Some(default_value) = info.default_value {
19+
impl FlbConfigParameterInfo {
20+
pub fn to_hover(&self, markup_kind: MarkupKind) -> Hover {
21+
let mut value = self.description.clone();
22+
if let Some(default_value) = &self.default_value {
2323
value.push_str(format!("\n\n(Default: `{}`)", default_value).as_str());
2424
}
2525

26-
MarkupContent {
27-
kind: MarkupKind::Markdown,
28-
value,
26+
Hover {
27+
contents: HoverContents::Markup(MarkupContent {
28+
kind: markup_kind,
29+
value,
30+
}),
31+
range: None,
2932
}
3033
}
3134
}
@@ -106,13 +109,30 @@ impl FlbCompletionSnippet {
106109

107110
ret
108111
}
112+
113+
// TODO: cache
114+
pub fn documentation_plaintext(&self) -> String {
115+
let mut ret = format!("{}: {}\n\n", self.plugin_name, self.label);
116+
117+
ret.push_str("[parameters]\n");
118+
for param in &self.config_params {
119+
ret.push_str(format!("- {}: {}\n", param.key, param.info.description).as_str());
120+
}
121+
122+
ret
123+
}
109124
}
110125

111126
pub fn snippet_to_completion(
112127
snippet: FlbCompletionSnippet,
113128
section_type: &FlbSectionType,
129+
markup_kind: MarkupKind,
114130
) -> CompletionItem {
115131
let insert_text = snippet.props_to_insert_text();
132+
let documentation_string = match markup_kind {
133+
MarkupKind::PlainText => snippet.documentation_plaintext(),
134+
MarkupKind::Markdown => snippet.documentation_markdown,
135+
};
116136

117137
CompletionItem {
118138
kind: Some(CompletionItemKind::SNIPPET),
@@ -122,8 +142,8 @@ pub fn snippet_to_completion(
122142
description: Some(format!("{} plugin", section_type)),
123143
}),
124144
documentation: Some(Documentation::MarkupContent(MarkupContent {
125-
kind: MarkupKind::Markdown,
126-
value: snippet.documentation_markdown,
145+
kind: markup_kind,
146+
value: documentation_string,
127147
})),
128148
insert_text_mode: Some(InsertTextMode::ADJUST_INDENTATION),
129149
insert_text_format: Some(InsertTextFormat::SNIPPET),
@@ -234,12 +254,15 @@ macro_rules! add_snippet {
234254

235255
include!("schema.generated.rs");
236256

237-
pub fn get_completion(section_type: &FlbSectionType) -> Vec<CompletionItem> {
257+
pub fn get_completion(
258+
section_type: &FlbSectionType,
259+
markup_kind: MarkupKind,
260+
) -> Vec<CompletionItem> {
238261
FLB_DATA
239262
.get_snippets(section_type)
240263
.unwrap_or(&vec![])
241264
.iter()
242-
.map(|snippet| snippet_to_completion(snippet.clone(), section_type))
265+
.map(|snippet| snippet_to_completion(snippet.clone(), section_type, markup_kind.clone()))
243266
.collect()
244267
}
245268

fluent-bit-language-server/src/language_server.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ use tower_lsp::{
1010
CompletionResponse, Diagnostic, DiagnosticOptions, DiagnosticServerCapabilities,
1111
DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams,
1212
DocumentDiagnosticParams, DocumentDiagnosticReport, DocumentDiagnosticReportResult,
13-
FullDocumentDiagnosticReport, Hover, HoverContents, HoverParams, HoverProviderCapability,
14-
InitializeParams, InitializeResult, InitializedParams, MessageType, Position, Range,
15-
RelatedFullDocumentDiagnosticReport, ServerCapabilities, TextDocumentContentChangeEvent,
16-
TextDocumentPositionParams, TextDocumentSyncCapability, TextDocumentSyncKind, Url,
13+
FullDocumentDiagnosticReport, Hover, HoverParams, HoverProviderCapability,
14+
InitializeParams, InitializeResult, InitializedParams, MarkupKind, MessageType, Position,
15+
Range, RelatedFullDocumentDiagnosticReport, ServerCapabilities,
16+
TextDocumentContentChangeEvent, TextDocumentPositionParams, TextDocumentSyncCapability,
17+
TextDocumentSyncKind, Url,
1718
},
1819
Client, LanguageServer,
1920
};
@@ -27,9 +28,15 @@ use crate::{
2728
pub struct Backend {
2829
pub(crate) client: Client,
2930
pub(crate) map: Arc<RwLock<HashMap<Url, TextDocument>>>,
31+
pub(crate) markup_kind: Arc<RwLock<MarkupKind>>,
3032
}
3133

3234
impl Backend {
35+
pub async fn set_markup_kind(&self, kind: MarkupKind) {
36+
let mut wr = self.markup_kind.write().await;
37+
*wr = kind;
38+
}
39+
3340
pub async fn open_file(&self, url: &Url, source_code: &str) {
3441
let mut wr = self.map.write().await;
3542
wr.insert(url.clone(), TextDocument::new(source_code));
@@ -221,7 +228,20 @@ impl Backend {
221228

222229
#[tower_lsp::async_trait]
223230
impl LanguageServer for Backend {
224-
async fn initialize(&self, _: InitializeParams) -> JsonRpcResult<InitializeResult> {
231+
async fn initialize(&self, params: InitializeParams) -> JsonRpcResult<InitializeResult> {
232+
params
233+
.capabilities
234+
.general
235+
.and_then(|c| c.markdown)
236+
.map(|_| self.set_markup_kind(MarkupKind::Markdown));
237+
238+
// self.client
239+
// .log_message(
240+
// MessageType::INFO,
241+
// serde_json::to_string(&params).unwrap().to_string(),
242+
// )
243+
// .await;
244+
225245
Ok(InitializeResult {
226246
server_info: None,
227247
capabilities: ServerCapabilities {
@@ -335,10 +355,8 @@ impl LanguageServer for Backend {
335355
.await?;
336356
let param_info = get_hover_info(&section_type, &key)?;
337357

338-
Some(Hover {
339-
contents: HoverContents::Markup(param_info.into()),
340-
range: None,
341-
})
358+
let markup_kind = self.markup_kind.read().await.clone();
359+
Some(param_info.to_hover(markup_kind))
342360
}
343361
.await;
344362

@@ -374,7 +392,8 @@ impl LanguageServer for Backend {
374392
.await;
375393

376394
if let Some(section) = section_type {
377-
ret.extend(get_completion(&section));
395+
let markup_kind = self.markup_kind.read().await.clone();
396+
ret.extend(get_completion(&section, markup_kind));
378397
} else {
379398
return Ok(None);
380399
}

fluent-bit-language-server/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{collections::HashMap, sync::Arc};
22

33
use tokio::sync::RwLock;
4-
use tower_lsp::{LspService, Server};
4+
use tower_lsp::{lsp_types::MarkupKind, LspService, Server};
55

66
use crate::language_server::Backend;
77

@@ -18,6 +18,7 @@ async fn main() {
1818
let (service, socket) = LspService::build(|client| Backend {
1919
client,
2020
map: Arc::new(RwLock::new(HashMap::new())),
21+
markup_kind: Arc::new(RwLock::new(MarkupKind::PlainText)),
2122
})
2223
.finish();
2324

0 commit comments

Comments
 (0)