Skip to content

Commit

Permalink
feat: support plaintext-only client (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cho authored Oct 19, 2024
1 parent 66ea942 commit 6b3a421
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
49 changes: 36 additions & 13 deletions fluent-bit-language-server/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use flb_schema::section::FlbSectionType;
#[allow(unused_imports)]
use once_cell::sync::Lazy;
use tower_lsp::lsp_types::{
CompletionItem, CompletionItemKind, CompletionItemLabelDetails, Documentation,
InsertTextFormat, InsertTextMode, MarkupContent, MarkupKind,
CompletionItem, CompletionItemKind, CompletionItemLabelDetails, Documentation, Hover,
HoverContents, InsertTextFormat, InsertTextMode, MarkupContent, MarkupKind,
};

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

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

MarkupContent {
kind: MarkupKind::Markdown,
value,
Hover {
contents: HoverContents::Markup(MarkupContent {
kind: markup_kind,
value,
}),
range: None,
}
}
}
Expand Down Expand Up @@ -106,13 +109,30 @@ impl FlbCompletionSnippet {

ret
}

// TODO: cache
pub fn documentation_plaintext(&self) -> String {
let mut ret = format!("{}: {}\n\n", self.plugin_name, self.label);

ret.push_str("[parameters]\n");
for param in &self.config_params {
ret.push_str(format!("- {}: {}\n", param.key, param.info.description).as_str());
}

ret
}
}

pub fn snippet_to_completion(
snippet: FlbCompletionSnippet,
section_type: &FlbSectionType,
markup_kind: MarkupKind,
) -> CompletionItem {
let insert_text = snippet.props_to_insert_text();
let documentation_string = match markup_kind {
MarkupKind::PlainText => snippet.documentation_plaintext(),
MarkupKind::Markdown => snippet.documentation_markdown,
};

CompletionItem {
kind: Some(CompletionItemKind::SNIPPET),
Expand All @@ -122,8 +142,8 @@ pub fn snippet_to_completion(
description: Some(format!("{} plugin", section_type)),
}),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: snippet.documentation_markdown,
kind: markup_kind,
value: documentation_string,
})),
insert_text_mode: Some(InsertTextMode::ADJUST_INDENTATION),
insert_text_format: Some(InsertTextFormat::SNIPPET),
Expand Down Expand Up @@ -234,12 +254,15 @@ macro_rules! add_snippet {

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

pub fn get_completion(section_type: &FlbSectionType) -> Vec<CompletionItem> {
pub fn get_completion(
section_type: &FlbSectionType,
markup_kind: MarkupKind,
) -> Vec<CompletionItem> {
FLB_DATA
.get_snippets(section_type)
.unwrap_or(&vec![])
.iter()
.map(|snippet| snippet_to_completion(snippet.clone(), section_type))
.map(|snippet| snippet_to_completion(snippet.clone(), section_type, markup_kind.clone()))
.collect()
}

Expand Down
39 changes: 29 additions & 10 deletions fluent-bit-language-server/src/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use tower_lsp::{
CompletionResponse, Diagnostic, DiagnosticOptions, DiagnosticServerCapabilities,
DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams,
DocumentDiagnosticParams, DocumentDiagnosticReport, DocumentDiagnosticReportResult,
FullDocumentDiagnosticReport, Hover, HoverContents, HoverParams, HoverProviderCapability,
InitializeParams, InitializeResult, InitializedParams, MessageType, Position, Range,
RelatedFullDocumentDiagnosticReport, ServerCapabilities, TextDocumentContentChangeEvent,
TextDocumentPositionParams, TextDocumentSyncCapability, TextDocumentSyncKind, Url,
FullDocumentDiagnosticReport, Hover, HoverParams, HoverProviderCapability,
InitializeParams, InitializeResult, InitializedParams, MarkupKind, MessageType, Position,
Range, RelatedFullDocumentDiagnosticReport, ServerCapabilities,
TextDocumentContentChangeEvent, TextDocumentPositionParams, TextDocumentSyncCapability,
TextDocumentSyncKind, Url,
},
Client, LanguageServer,
};
Expand All @@ -27,9 +28,15 @@ use crate::{
pub struct Backend {
pub(crate) client: Client,
pub(crate) map: Arc<RwLock<HashMap<Url, TextDocument>>>,
pub(crate) markup_kind: Arc<RwLock<MarkupKind>>,
}

impl Backend {
pub async fn set_markup_kind(&self, kind: MarkupKind) {
let mut wr = self.markup_kind.write().await;
*wr = kind;
}

pub async fn open_file(&self, url: &Url, source_code: &str) {
let mut wr = self.map.write().await;
wr.insert(url.clone(), TextDocument::new(source_code));
Expand Down Expand Up @@ -221,7 +228,20 @@ impl Backend {

#[tower_lsp::async_trait]
impl LanguageServer for Backend {
async fn initialize(&self, _: InitializeParams) -> JsonRpcResult<InitializeResult> {
async fn initialize(&self, params: InitializeParams) -> JsonRpcResult<InitializeResult> {
params
.capabilities
.general
.and_then(|c| c.markdown)
.map(|_| self.set_markup_kind(MarkupKind::Markdown));

// self.client
// .log_message(
// MessageType::INFO,
// serde_json::to_string(&params).unwrap().to_string(),
// )
// .await;

Ok(InitializeResult {
server_info: None,
capabilities: ServerCapabilities {
Expand Down Expand Up @@ -335,10 +355,8 @@ impl LanguageServer for Backend {
.await?;
let param_info = get_hover_info(&section_type, &key)?;

Some(Hover {
contents: HoverContents::Markup(param_info.into()),
range: None,
})
let markup_kind = self.markup_kind.read().await.clone();
Some(param_info.to_hover(markup_kind))
}
.await;

Expand Down Expand Up @@ -374,7 +392,8 @@ impl LanguageServer for Backend {
.await;

if let Some(section) = section_type {
ret.extend(get_completion(&section));
let markup_kind = self.markup_kind.read().await.clone();
ret.extend(get_completion(&section, markup_kind));
} else {
return Ok(None);
}
Expand Down
3 changes: 2 additions & 1 deletion fluent-bit-language-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, sync::Arc};

use tokio::sync::RwLock;
use tower_lsp::{LspService, Server};
use tower_lsp::{lsp_types::MarkupKind, LspService, Server};

use crate::language_server::Backend;

Expand All @@ -18,6 +18,7 @@ async fn main() {
let (service, socket) = LspService::build(|client| Backend {
client,
map: Arc::new(RwLock::new(HashMap::new())),
markup_kind: Arc::new(RwLock::new(MarkupKind::PlainText)),
})
.finish();

Expand Down

0 comments on commit 6b3a421

Please sign in to comment.