summaryrefslogtreecommitdiff
path: root/support/texlab/src/workspace/feature.rs
blob: 815f5e17247eb267ecc43943d8f168d5b057c0b5 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use super::{Document, DocumentView, Workspace, WorkspaceBuilder};
use futures_boxed::boxed;
use lsp_types::*;
use std::sync::Arc;

pub struct FeatureRequest<P> {
    pub params: P,
    pub view: DocumentView,
    pub client_capabilities: Arc<ClientCapabilities>,
    pub distribution: Arc<Box<dyn tex::Distribution>>,
}

impl<P> FeatureRequest<P> {
    pub fn workspace(&self) -> &Workspace {
        &self.view.workspace
    }

    pub fn document(&self) -> &Document {
        &self.view.document
    }

    pub fn related_documents(&self) -> &[Arc<Document>] {
        &self.view.related_documents
    }
}

pub trait FeatureProvider {
    type Params;
    type Output;

    #[boxed]
    async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output;
}

type ListProvider<P, O> = Box<dyn FeatureProvider<Params = P, Output = Vec<O>> + Send + Sync>;

#[derive(Default)]
pub struct ConcatProvider<P, O> {
    providers: Vec<ListProvider<P, O>>,
}

impl<P, O> ConcatProvider<P, O> {
    pub fn new(providers: Vec<ListProvider<P, O>>) -> Self {
        Self { providers }
    }
}

impl<P, O> FeatureProvider for ConcatProvider<P, O>
where
    P: Send + Sync,
    O: Send + Sync,
{
    type Params = P;
    type Output = Vec<O>;

    #[boxed]
    async fn execute<'a>(&'a self, request: &'a FeatureRequest<P>) -> Vec<O> {
        let mut items = Vec::new();
        for provider in &self.providers {
            items.append(&mut provider.execute(request).await);
        }
        items
    }
}

type OptionProvider<P, O> = Box<dyn FeatureProvider<Params = P, Output = Option<O>> + Send + Sync>;

#[derive(Default)]
pub struct ChoiceProvider<P, O> {
    providers: Vec<OptionProvider<P, O>>,
}

impl<P, O> ChoiceProvider<P, O> {
    pub fn new(providers: Vec<OptionProvider<P, O>>) -> Self {
        Self { providers }
    }
}

impl<P, O> FeatureProvider for ChoiceProvider<P, O>
where
    P: Send + Sync,
    O: Send + Sync,
{
    type Params = P;
    type Output = Option<O>;

    #[boxed]
    async fn execute<'a>(&'a self, request: &'a FeatureRequest<P>) -> Option<O> {
        for provider in &self.providers {
            let item = provider.execute(request).await;
            if item.is_some() {
                return item;
            }
        }
        None
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct FeatureSpecFile {
    name: &'static str,
    text: &'static str,
}

pub struct FeatureSpec {
    pub files: Vec<FeatureSpecFile>,
    pub main_file: &'static str,
    pub position: Position,
    pub new_name: &'static str,
    pub include_declaration: bool,
    pub client_capabilities: ClientCapabilities,
    pub distribution: Box<dyn tex::Distribution>,
}

impl Default for FeatureSpec {
    fn default() -> Self {
        Self {
            files: Vec::new(),
            main_file: "",
            position: Position::new(0, 0),
            new_name: "",
            include_declaration: false,
            client_capabilities: ClientCapabilities::default(),
            distribution: Box::new(tex::Unknown::default()),
        }
    }
}

impl FeatureSpec {
    pub fn file(name: &'static str, text: &'static str) -> FeatureSpecFile {
        FeatureSpecFile { name, text }
    }

    pub fn uri(name: &str) -> Url {
        let path = std::env::temp_dir().join(name);
        Url::from_file_path(path).unwrap()
    }

    fn identifier(&self) -> TextDocumentIdentifier {
        let uri = Self::uri(self.main_file);
        TextDocumentIdentifier::new(uri)
    }

    fn view(&self) -> DocumentView {
        let mut builder = WorkspaceBuilder::new();
        for file in &self.files {
            builder.document(file.name, file.text);
        }
        let workspace = builder.workspace;
        let main_uri = Self::uri(self.main_file);
        let main_document = workspace.find(&main_uri.into()).unwrap();
        DocumentView::new(Arc::new(workspace), main_document)
    }

    fn request<T>(self, params: T) -> FeatureRequest<T> {
        FeatureRequest {
            params,
            view: self.view(),
            client_capabilities: Arc::new(self.client_capabilities),
            distribution: Arc::new(self.distribution),
        }
    }
}

impl Into<FeatureRequest<TextDocumentPositionParams>> for FeatureSpec {
    fn into(self) -> FeatureRequest<TextDocumentPositionParams> {
        let params = TextDocumentPositionParams::new(self.identifier(), self.position);
        self.request(params)
    }
}

impl Into<FeatureRequest<CompletionParams>> for FeatureSpec {
    fn into(self) -> FeatureRequest<CompletionParams> {
        let params = CompletionParams {
            text_document_position: TextDocumentPositionParams::new(
                self.identifier(),
                self.position,
            ),
            context: None,
        };
        self.request(params)
    }
}

impl Into<FeatureRequest<FoldingRangeParams>> for FeatureSpec {
    fn into(self) -> FeatureRequest<FoldingRangeParams> {
        let params = FoldingRangeParams {
            text_document: self.identifier(),
        };
        self.request(params)
    }
}

impl Into<FeatureRequest<DocumentLinkParams>> for FeatureSpec {
    fn into(self) -> FeatureRequest<DocumentLinkParams> {
        let params = DocumentLinkParams {
            text_document: self.identifier(),
        };
        self.request(params)
    }
}

impl Into<FeatureRequest<ReferenceParams>> for FeatureSpec {
    fn into(self) -> FeatureRequest<ReferenceParams> {
        let params = ReferenceParams {
            text_document_position: TextDocumentPositionParams::new(
                self.identifier(),
                self.position,
            ),
            context: ReferenceContext {
                include_declaration: self.include_declaration,
            },
        };
        self.request(params)
    }
}

impl Into<FeatureRequest<RenameParams>> for FeatureSpec {
    fn into(self) -> FeatureRequest<RenameParams> {
        let params = RenameParams {
            text_document_position: TextDocumentPositionParams::new(
                self.identifier(),
                self.position,
            ),
            new_name: self.new_name.to_owned(),
        };
        self.request(params)
    }
}

impl Into<FeatureRequest<DocumentSymbolParams>> for FeatureSpec {
    fn into(self) -> FeatureRequest<DocumentSymbolParams> {
        let params = DocumentSymbolParams {
            text_document: self.identifier(),
        };
        self.request(params)
    }
}

pub fn test_feature<F, P, O, S>(provider: F, spec: S) -> O
where
    F: FeatureProvider<Params = P, Output = O>,
    S: Into<FeatureRequest<P>>,
{
    futures::executor::block_on(provider.execute(&spec.into()))
}