sway_lsp/
server.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! This module implements the [LanguageServer] trait for [ServerState].
//! It provides an interface between the LSP protocol and the sway-lsp internals.

use crate::{
    handlers::{notification, request},
    lsp_ext::{MetricsParams, OnEnterParams, ShowAstParams, VisualizeParams},
    server_state::ServerState,
};
use lsp_types::{
    CodeActionParams, CodeActionResponse, CodeLens, CodeLensParams, CompletionParams,
    CompletionResponse, DidChangeTextDocumentParams, DidChangeWatchedFilesParams,
    DidCloseTextDocumentParams, DidOpenTextDocumentParams, DidSaveTextDocumentParams,
    DocumentFormattingParams, DocumentHighlight, DocumentHighlightParams, DocumentSymbolParams,
    DocumentSymbolResponse, GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverParams,
    InitializeParams, InitializeResult, InitializedParams, InlayHint, InlayHintParams, Location,
    PrepareRenameResponse, ReferenceParams, RenameParams, SemanticTokensParams,
    SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult,
    TextDocumentIdentifier, TextDocumentPositionParams, TextEdit, WorkspaceEdit,
};
use sway_utils::PerformanceData;
use tower_lsp::{jsonrpc::Result, LanguageServer};

#[tower_lsp::async_trait]
impl LanguageServer for ServerState {
    async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
        request::handle_initialize(self, &params)
    }

    async fn initialized(&self, _: InitializedParams) {
        let _p = tracing::trace_span!("parse_text").entered();
        tracing::info!("Sway Language Server Initialized");
    }

    async fn shutdown(&self) -> Result<()> {
        self.shutdown_server()
    }

    async fn did_open(&self, params: DidOpenTextDocumentParams) {
        if let Err(err) = notification::handle_did_open_text_document(self, params).await {
            tracing::error!("{}", err.to_string());
        }
    }

    async fn did_close(&self, params: DidCloseTextDocumentParams) {
        if let Err(err) = self
            .pid_locked_files
            .remove_dirty_flag(&params.text_document.uri)
        {
            tracing::error!("{}", err.to_string());
        }
    }

    async fn did_change(&self, params: DidChangeTextDocumentParams) {
        if let Err(err) = notification::handle_did_change_text_document(self, params).await {
            tracing::error!("{}", err.to_string());
        }
    }

    async fn did_save(&self, params: DidSaveTextDocumentParams) {
        if let Err(err) = notification::handle_did_save_text_document(self, params).await {
            tracing::error!("{}", err.to_string());
        }
    }

    async fn did_change_watched_files(&self, params: DidChangeWatchedFilesParams) {
        if let Err(err) = notification::handle_did_change_watched_files(self, params).await {
            tracing::error!("{}", err.to_string());
        }
    }

    async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
        request::handle_hover(self, params).await
    }

    async fn code_action(&self, params: CodeActionParams) -> Result<Option<CodeActionResponse>> {
        request::handle_code_action(self, params).await
    }

    async fn code_lens(&self, params: CodeLensParams) -> Result<Option<Vec<CodeLens>>> {
        request::handle_code_lens(self, params).await
    }

    async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> {
        request::handle_completion(self, params).await
    }

    async fn document_symbol(
        &self,
        params: DocumentSymbolParams,
    ) -> Result<Option<DocumentSymbolResponse>> {
        request::handle_document_symbol(self, params).await
    }

    async fn semantic_tokens_full(
        &self,
        params: SemanticTokensParams,
    ) -> Result<Option<SemanticTokensResult>> {
        request::handle_semantic_tokens_full(self, params).await
    }

    async fn semantic_tokens_range(
        &self,
        params: SemanticTokensRangeParams,
    ) -> Result<Option<SemanticTokensRangeResult>> {
        request::handle_semantic_tokens_range(self, params).await
    }

    async fn document_highlight(
        &self,
        params: DocumentHighlightParams,
    ) -> Result<Option<Vec<DocumentHighlight>>> {
        request::handle_document_highlight(self, params).await
    }

    async fn goto_definition(
        &self,
        params: GotoDefinitionParams,
    ) -> Result<Option<GotoDefinitionResponse>> {
        request::handle_goto_definition(self, params).await
    }

    async fn formatting(&self, params: DocumentFormattingParams) -> Result<Option<Vec<TextEdit>>> {
        request::handle_formatting(self, params).await
    }

    async fn rename(&self, params: RenameParams) -> Result<Option<WorkspaceEdit>> {
        request::handle_rename(self, params).await
    }

    async fn prepare_rename(
        &self,
        params: TextDocumentPositionParams,
    ) -> Result<Option<PrepareRenameResponse>> {
        request::handle_prepare_rename(self, params).await
    }

    async fn inlay_hint(&self, params: InlayHintParams) -> Result<Option<Vec<InlayHint>>> {
        request::handle_inlay_hints(self, params).await
    }

    async fn references(&self, params: ReferenceParams) -> Result<Option<Vec<Location>>> {
        request::handle_references(self, params).await
    }
}

// Custom LSP-Server Methods
impl ServerState {
    pub async fn show_ast(&self, params: ShowAstParams) -> Result<Option<TextDocumentIdentifier>> {
        request::handle_show_ast(self, params).await
    }

    pub async fn on_enter(&self, params: OnEnterParams) -> Result<Option<WorkspaceEdit>> {
        request::handle_on_enter(self, params).await
    }

    pub async fn visualize(&self, params: VisualizeParams) -> Result<Option<String>> {
        request::handle_visualize(self, params)
    }

    pub async fn metrics(
        &self,
        params: MetricsParams,
    ) -> Result<Option<Vec<(String, PerformanceData)>>> {
        request::metrics(self, params).await
    }
}