Skip to content

Commit 07df380

Browse files
ndmitchellfacebook-github-bot
authored andcommitted
Make find_call_name return a Span
Summary: If we ever want to do further manipulations (e.g. find hover locations) then a Span is better than a ResolvedSpan. Reviewed By: milend Differential Revision: D47190448 fbshipit-source-id: a09242bc150c9f5b9a1f7ebf3e9afa52e3418dbf
1 parent b74b293 commit 07df380

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

starlark/src/analysis/find_call_name.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
use crate::codemap::ResolvedSpan;
1918
use crate::codemap::Span;
2019
use crate::codemap::Spanned;
2120
use crate::syntax::ast::Argument;
@@ -30,7 +29,7 @@ impl AstModule {
3029
///
3130
/// NOTE: If the AST is exposed in the future, this function may be removed and implemented
3231
/// by specific programs instead.
33-
pub fn find_function_call_with_name(&self, name: &str) -> Option<ResolvedSpan> {
32+
pub fn find_function_call_with_name(&self, name: &str) -> Option<Span> {
3433
let mut ret = None;
3534

3635
fn visit_expr(ret: &mut Option<Span>, name: &str, node: &AstExpr) {
@@ -64,7 +63,7 @@ impl AstModule {
6463
}
6564

6665
self.statement.visit_expr(|x| visit_expr(&mut ret, name, x));
67-
ret.map(|span| self.codemap.resolve_span(span))
66+
ret
6867
}
6968
}
7069

@@ -95,7 +94,9 @@ def x(name = "foo_name"):
9594
end_line: 1,
9695
end_column: 3
9796
}),
98-
module.find_function_call_with_name("foo_name")
97+
module
98+
.find_function_call_with_name("foo_name")
99+
.map(|span| module.codemap.resolve_span(span))
99100
);
100101
assert_eq!(None, module.find_function_call_with_name("bar_name"));
101102
Ok(())

starlark/src/lsp/server.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use serde::Serialize;
6767
use serde::Serializer;
6868

6969
use crate::codemap::ResolvedSpan;
70+
use crate::codemap::Span;
7071
use crate::lsp::definition::Definition;
7172
use crate::lsp::definition::DottedDefinition;
7273
use crate::lsp::definition::IdentifierDefinition;
@@ -219,8 +220,7 @@ pub struct StringLiteralResult {
219220
///
220221
/// If `None`, then just jump to the URL. Do not attempt to load the file.
221222
#[derivative(Debug = "ignore")]
222-
pub location_finder:
223-
Option<Box<dyn FnOnce(&AstModule) -> anyhow::Result<Option<Range>> + Send>>,
223+
pub location_finder: Option<Box<dyn FnOnce(&AstModule) -> anyhow::Result<Option<Span>> + Send>>,
224224
}
225225

226226
fn _assert_string_literal_result_is_send() {
@@ -498,7 +498,9 @@ impl<T: LspContext> Backend<T> {
498498
let result =
499499
self.get_ast_or_load_from_disk(&url)
500500
.and_then(|ast| match ast {
501-
Some(module) => location_finder(&module.ast),
501+
Some(module) => location_finder(&module.ast).map(|span| {
502+
span.map(|span| module.ast.codemap.resolve_span(span))
503+
}),
502504
None => Ok(None),
503505
});
504506
if let Err(e) = &result {
@@ -1358,10 +1360,10 @@ mod test {
13581360

13591361
let bar_contents = r#""Just <bar>a string</bar>""#;
13601362
let bar = FixtureWithRanges::from_fixture(bar_uri.path(), bar_contents)?;
1361-
let bar_range = bar.resolved_span("bar");
1362-
let bar_range_str = format!(
1363-
"{}:{}:{}:{}",
1364-
bar_range.begin_line, bar_range.begin_column, bar_range.end_line, bar_range.end_column
1363+
let bar_resolved_span = bar.resolved_span("bar");
1364+
let bar_span_str = format!(
1365+
"{}:{}",
1366+
bar_resolved_span.begin_column, bar_resolved_span.end_column
13651367
);
13661368

13671369
let foo_contents = dedent(
@@ -1442,7 +1444,7 @@ mod test {
14421444
"#,
14431445
)
14441446
.trim()
1445-
.replace("{bar_range}", &bar_range_str);
1447+
.replace("{bar_range}", &bar_span_str);
14461448
let foo = FixtureWithRanges::from_fixture(foo_uri.path(), &foo_contents)?;
14471449

14481450
let mut server = TestServer::new()?;
@@ -1451,7 +1453,7 @@ mod test {
14511453

14521454
let mut test = |name: &str, expect_range: bool| -> anyhow::Result<()> {
14531455
let range = if expect_range {
1454-
bar_range
1456+
bar_resolved_span
14551457
} else {
14561458
Default::default()
14571459
};

starlark/src/lsp/test.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ use lsp_types::GotoCapability;
4747
use lsp_types::InitializeParams;
4848
use lsp_types::InitializeResult;
4949
use lsp_types::InitializedParams;
50-
use lsp_types::Position;
51-
use lsp_types::Range;
5250
use lsp_types::TextDocumentClientCapabilities;
5351
use lsp_types::TextDocumentContentChangeEvent;
5452
use lsp_types::TextDocumentItem;
@@ -57,6 +55,8 @@ use lsp_types::VersionedTextDocumentIdentifier;
5755
use maplit::hashmap;
5856
use serde::de::DeserializeOwned;
5957

58+
use crate::codemap::Pos;
59+
use crate::codemap::Span;
6060
use crate::docs::render_docs_as_code;
6161
use crate::docs::Doc;
6262
use crate::docs::DocFunction;
@@ -170,20 +170,15 @@ impl LspContext for TestServerContext {
170170
literal: &str,
171171
current_file: &LspUrl,
172172
) -> anyhow::Result<Option<StringLiteralResult>> {
173-
let re = regex::Regex::new(r#"--(\d+):(\d+):(\d+):(\d+)$"#)?;
174-
let (literal, range) = match re.captures(literal) {
173+
let re = regex::Regex::new(r#"--(\d+):(\d+)$"#)?;
174+
let (literal, span) = match re.captures(literal) {
175175
Some(cap) => {
176-
let start_line = cap.get(1).unwrap().as_str().parse().unwrap();
177-
let start_col = cap.get(2).unwrap().as_str().parse().unwrap();
178-
let end_line = cap.get(3).unwrap().as_str().parse().unwrap();
179-
let end_col = cap.get(4).unwrap().as_str().parse().unwrap();
180-
let range = Range::new(
181-
Position::new(start_line, start_col),
182-
Position::new(end_line, end_col),
183-
);
176+
let start_pos = cap.get(1).unwrap().as_str().parse().unwrap();
177+
let end_pos = cap.get(2).unwrap().as_str().parse().unwrap();
178+
let span = Span::new(Pos::new(start_pos), Pos::new(end_pos));
184179
(
185180
literal[0..cap.get(0).unwrap().start()].to_owned(),
186-
Some(range),
181+
Some(span),
187182
)
188183
}
189184
None => (literal.to_owned(), None),
@@ -193,7 +188,7 @@ impl LspContext for TestServerContext {
193188
LspUrl::File(u) => match u.extension() {
194189
Some(e) if e == "star" => Some(StringLiteralResult {
195190
url,
196-
location_finder: Some(Box::new(move |_ast| Ok(range))),
191+
location_finder: Some(Box::new(move |_ast| Ok(span))),
197192
}),
198193
_ => Some(StringLiteralResult {
199194
url,

0 commit comments

Comments
 (0)