-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy patheval.rs
More file actions
389 lines (359 loc) · 12.6 KB
/
Copy patheval.rs
File metadata and controls
389 lines (359 loc) · 12.6 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright 2019 The Starlark in Rust Authors.
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
use std::io;
use std::iter;
use std::path::Path;
use std::path::PathBuf;
use itertools::Either;
use lsp_types::Url;
use starlark::analysis::AstModuleLint;
use starlark::docs::get_registered_starlark_docs;
use starlark::docs::render_docs_as_code;
use starlark::docs::Doc;
use starlark::docs::DocItem;
use starlark::docs::DocModule;
use starlark::environment::FrozenModule;
use starlark::environment::Globals;
use starlark::environment::Module;
use starlark::errors::EvalMessage;
use starlark::eval::Evaluator;
use starlark::syntax::AstModule;
use starlark::syntax::Dialect;
use starlark_lsp::error::eval_message_to_lsp_diagnostic;
use starlark_lsp::server::LspContext;
use starlark_lsp::server::LspEvalResult;
use starlark_lsp::server::LspUrl;
use starlark_lsp::server::StringLiteralResult;
#[derive(Debug)]
pub(crate) enum ContextMode {
Check,
Run,
}
#[derive(Debug, thiserror::Error)]
enum ContextError {
/// The provided Url was not absolute and it needs to be.
#[error("Path for URL `{}` was not absolute", .0)]
NotAbsolute(LspUrl),
/// The scheme provided was not correct or supported.
#[error("Url `{}` was expected to be of type `{}`", .1, .0)]
WrongScheme(String, LspUrl),
}
#[derive(Debug)]
pub(crate) struct Context {
pub(crate) mode: ContextMode,
pub(crate) print_non_none: bool,
pub(crate) prelude: Vec<FrozenModule>,
pub(crate) module: Option<Module>,
pub(crate) dialect: Dialect,
pub(crate) globals: Globals,
pub(crate) builtin_docs: HashMap<LspUrl, String>,
pub(crate) builtin_symbols: HashMap<String, LspUrl>,
pub(crate) eager: bool,
}
/// The outcome of evaluating (checking, parsing or running) given starlark code.
pub(crate) struct EvalResult<T: Iterator<Item = EvalMessage>> {
/// The diagnostic and error messages from evaluating a given piece of starlark code.
pub messages: T,
/// If the code is only parsed, not run, and there were no errors, this will contain
/// the parsed module. Otherwise, it will be `None`
pub ast: Option<AstModule>,
}
/// Errors when [`LspContext::resolve_load()`] cannot resolve a given path.
#[derive(thiserror::Error, Debug)]
enum ResolveLoadError {
/// Attempted to resolve a relative path, but no current_file_path was provided,
/// so it is not known what to resolve the path against.
#[error("Relative path `{}` provided, but current_file_path could not be determined", .0.display())]
MissingCurrentFilePath(PathBuf),
/// The scheme provided was not correct or supported.
#[error("Url `{}` was expected to be of type `{}`", .1, .0)]
WrongScheme(String, LspUrl),
}
impl Context {
pub(crate) fn new(
mode: ContextMode,
print_non_none: bool,
prelude: &[PathBuf],
module: bool,
dialect: Dialect,
globals: Globals,
eager: bool,
) -> anyhow::Result<Self> {
let prelude: Vec<_> = prelude
.iter()
.map(|x| {
let env = Module::new();
{
let mut eval = Evaluator::new(&env);
let module =
AstModule::parse_file(x, &dialect).map_err(starlark::Error::into_anyhow)?;
eval.eval_module(module, &globals)
.map_err(starlark::Error::into_anyhow)?;
}
env.freeze()
})
.collect::<anyhow::Result<_>>()?;
let module = if module {
Some(Self::new_module(&prelude))
} else {
None
};
let mut builtins: HashMap<LspUrl, Vec<Doc>> = HashMap::new();
let mut builtin_symbols: HashMap<String, LspUrl> = HashMap::new();
for doc in get_registered_starlark_docs() {
let uri = Self::url_for_doc(&doc);
builtin_symbols.insert(doc.id.name.clone(), uri.clone());
builtins.entry(uri).or_default().push(doc);
}
let builtin_docs = builtins
.into_iter()
.map(|(u, ds)| (u, render_docs_as_code(&ds)))
.collect();
Ok(Self {
mode,
print_non_none,
prelude,
module,
dialect,
globals,
builtin_docs,
builtin_symbols,
eager,
})
}
fn url_for_doc(doc: &Doc) -> LspUrl {
let url = match &doc.item {
DocItem::Module(_) => Url::parse("starlark:/native/builtins.bzl").unwrap(),
DocItem::Object(_) => {
Url::parse(&format!("starlark:/native/builtins/{}.bzl", doc.id.name)).unwrap()
}
DocItem::Function(_) | DocItem::Property(_) => {
Url::parse("starlark:/native/builtins.bzl").unwrap()
}
};
LspUrl::try_from(url).unwrap()
}
fn new_module(prelude: &[FrozenModule]) -> Module {
let module = Module::new();
for p in prelude {
module.import_public_symbols(p);
}
module
}
fn go(&self, file: &str, ast: AstModule) -> EvalResult<impl Iterator<Item = EvalMessage>> {
let mut warnings = Either::Left(iter::empty());
let mut errors = Either::Left(iter::empty());
let final_ast = match self.mode {
ContextMode::Check => {
warnings = Either::Right(self.check(&ast));
Some(ast)
}
ContextMode::Run => {
errors = Either::Right(self.run(file, ast).messages);
None
}
};
EvalResult {
messages: warnings.chain(errors),
ast: final_ast,
}
}
// Convert a result over iterator of EvalMessage, into an iterator of EvalMessage
fn err(
file: &str,
result: starlark::Result<EvalResult<impl Iterator<Item = EvalMessage>>>,
) -> EvalResult<impl Iterator<Item = EvalMessage>> {
match result {
Err(e) => EvalResult {
messages: Either::Left(iter::once(EvalMessage::from_error(Path::new(file), &e))),
ast: None,
},
Ok(res) => EvalResult {
messages: Either::Right(res.messages),
ast: res.ast,
},
}
}
pub(crate) fn expression(
&self,
content: String,
) -> EvalResult<impl Iterator<Item = EvalMessage>> {
let file = "expression";
Self::err(
file,
AstModule::parse(file, content, &self.dialect)
.map(|module| self.go(file, module))
.map_err(Into::into),
)
}
pub(crate) fn file(&self, file: &Path) -> EvalResult<impl Iterator<Item = EvalMessage>> {
let filename = &file.to_string_lossy();
Self::err(
filename,
fs::read_to_string(file)
.map(|content| self.file_with_contents(filename, content))
.map_err(|e| anyhow::Error::from(e).into()),
)
}
pub(crate) fn file_with_contents(
&self,
filename: &str,
content: String,
) -> EvalResult<impl Iterator<Item = EvalMessage>> {
Self::err(
filename,
AstModule::parse(filename, content, &self.dialect)
.map(|module| self.go(filename, module))
.map_err(Into::into),
)
}
fn run(&self, file: &str, ast: AstModule) -> EvalResult<impl Iterator<Item = EvalMessage>> {
let new_module;
let module = match self.module.as_ref() {
Some(module) => module,
None => {
new_module = Self::new_module(&self.prelude);
&new_module
}
};
let mut eval = Evaluator::new(module);
eval.enable_terminal_breakpoint_console();
Self::err(
file,
eval.eval_module(ast, &self.globals)
.map(|v| {
if self.print_non_none && !v.is_none() {
println!("{}", v);
}
EvalResult {
messages: iter::empty(),
ast: None,
}
})
.map_err(Into::into),
)
}
fn check(&self, module: &AstModule) -> impl Iterator<Item = EvalMessage> {
let globals = if self.prelude.is_empty() {
None
} else {
let mut globals = HashSet::new();
for modu in &self.prelude {
for name in modu.names() {
globals.insert(name.as_str().to_owned());
}
}
for global_symbol in self.builtin_symbols.keys() {
globals.insert(global_symbol.to_owned());
}
Some(globals)
};
module
.lint(globals.as_ref())
.into_iter()
.map(EvalMessage::from)
}
}
impl LspContext for Context {
fn parse_file_with_contents(&self, uri: &LspUrl, content: String) -> LspEvalResult {
match uri {
LspUrl::File(uri) => {
let EvalResult { messages, ast } =
self.file_with_contents(&uri.to_string_lossy(), content);
LspEvalResult {
diagnostics: messages.map(eval_message_to_lsp_diagnostic).collect(),
ast,
}
}
_ => LspEvalResult::default(),
}
}
fn resolve_load(
&self,
path: &str,
current_file: &LspUrl,
_workspace_root: Option<&Path>,
) -> anyhow::Result<LspUrl> {
let path = PathBuf::from(path);
match current_file {
LspUrl::File(current_file_path) => {
let current_file_dir = current_file_path.parent();
let absolute_path = match (current_file_dir, path.is_absolute()) {
(_, true) => Ok(path),
(Some(current_file_dir), false) => Ok(current_file_dir.join(&path)),
(None, false) => Err(ResolveLoadError::MissingCurrentFilePath(path)),
}?;
Ok(Url::from_file_path(absolute_path).unwrap().try_into()?)
}
_ => Err(
ResolveLoadError::WrongScheme("file://".to_owned(), current_file.clone()).into(),
),
}
}
fn resolve_string_literal(
&self,
literal: &str,
current_file: &LspUrl,
workspace_root: Option<&Path>,
) -> anyhow::Result<Option<StringLiteralResult>> {
self.resolve_load(literal, current_file, workspace_root)
.map(|url| {
Some(StringLiteralResult {
url,
location_finder: None,
})
})
}
fn get_load_contents(&self, uri: &LspUrl) -> anyhow::Result<Option<String>> {
match uri {
LspUrl::File(path) => match path.is_absolute() {
true => match fs::read_to_string(path) {
Ok(contents) => Ok(Some(contents)),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
},
false => Err(ContextError::NotAbsolute(uri.clone()).into()),
},
LspUrl::Starlark(_) => Ok(self.builtin_docs.get(uri).cloned()),
_ => Err(ContextError::WrongScheme("file://".to_owned(), uri.clone()).into()),
}
}
fn get_url_for_global_symbol(
&self,
_current_file: &LspUrl,
symbol: &str,
) -> anyhow::Result<Option<LspUrl>> {
Ok(self.builtin_symbols.get(symbol).cloned())
}
fn render_as_load(
&self,
_target: &LspUrl,
_current_file: &LspUrl,
_workspace_root: Option<&Path>,
) -> anyhow::Result<String> {
Err(anyhow::anyhow!("Not yet implemented, render_as_load"))
}
fn get_environment(&self, _uri: &LspUrl) -> DocModule {
DocModule::default()
}
fn is_eager(&self) -> bool {
self.eager
}
}