Skip to content

Commit ea36bb1

Browse files
giacomocavalierilpil
authored andcommitted
add code_at utility function
1 parent 064d894 commit ea36bb1

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

compiler-core/src/language_server/code_action.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4663,6 +4663,13 @@ where
46634663
}
46644664
}
46654665

4666+
fn code_at(module: &Module, span: SrcSpan) -> &str {
4667+
module
4668+
.code
4669+
.get(span.start as usize..span.end as usize)
4670+
.expect("code location must be valid")
4671+
}
4672+
46664673
impl<'ast, IO> ast::visit::Visit<'ast> for PatternMatchOnValue<'ast, IO>
46674674
where
46684675
IO: CommandExecutor + FileSystemWriter + FileSystemReader + BeamCompiler + Clone,
@@ -5080,12 +5087,11 @@ impl<'a> GenerateFunction<'a> {
50805087
function_type: &Arc<Type>,
50815088
given_arguments: Option<&'a [TypedCallArg]>,
50825089
) {
5083-
let name_range = function_name_location.start as usize..function_name_location.end as usize;
5084-
let candidate_name = self.module.code.get(name_range);
5090+
let candidate_name = code_at(self.module, function_name_location);
50855091
match (candidate_name, function_type.fn_types()) {
5086-
(None, _) | (_, None) => (),
5087-
(Some(name), _) if !is_valid_lowercase_name(name) => (),
5088-
(Some(name), Some((arguments_types, return_type))) => {
5092+
(_, None) => (),
5093+
(name, _) if !is_valid_lowercase_name(name) => (),
5094+
(name, Some((arguments_types, return_type))) => {
50895095
self.function_to_generate = Some(FunctionToGenerate {
50905096
name,
50915097
arguments_types,
@@ -5438,8 +5444,7 @@ where
54385444
type_: &Arc<Type>,
54395445
given_arguments: Option<Arguments<'a>>,
54405446
) -> Option<VariantToGenerate<'a>> {
5441-
let name_range = function_name_location.start as usize..function_name_location.end as usize;
5442-
let name = self.module.code.get(name_range).expect("valid code range");
5447+
let name = code_at(self.module, function_name_location);
54435448
if !is_valid_uppercase_name(name) {
54445449
return None;
54455450
}
@@ -6149,16 +6154,18 @@ impl<'a> ConvertToPipe<'a> {
61496154
return vec![];
61506155
};
61516156

6152-
let arg_range = if arg.uses_label_shorthand() {
6153-
arg.location.start as usize..arg.location.end as usize - 1
6157+
let arg_location = if arg.uses_label_shorthand() {
6158+
SrcSpan {
6159+
start: arg.location.start,
6160+
end: arg.location.end - 1,
6161+
}
61546162
} else if arg.label.is_some() {
6155-
let value = arg.value.location();
6156-
value.start as usize..value.end as usize
6163+
arg.value.location()
61576164
} else {
6158-
arg.location.start as usize..arg.location.end as usize
6165+
arg.location
61596166
};
61606167

6161-
let arg_text = self.module.code.get(arg_range).expect("invalid srcspan");
6168+
let arg_text = code_at(self.module, arg_location);
61626169
let arg_text = match arg.value {
61636170
// If the expression being piped is a binary operation with
61646171
// precedence lower than pipes then we have to wrap it in curly
@@ -7774,7 +7781,7 @@ impl<'a> CollapseNestedCase<'a> {
77747781
// Notice one key detail: since alternative patterns can't be nested we
77757782
// can't simply write `Ok(2 | 3)` but we have to write `Ok(2) | Ok(3)`!
77767783

7777-
let pattern_text: String = self.code_at(matched_pattern_span).into();
7784+
let pattern_text: String = code_at(self.module, matched_pattern_span).into();
77787785
let matched_variable_span = matched_variable.location();
77797786

77807787
let pattern_with_variable = |new_content: String| {
@@ -7828,19 +7835,19 @@ impl<'a> CollapseNestedCase<'a> {
78287835
let pattern_location =
78297836
patterns.first().expect("must have a pattern").location();
78307837

7831-
let mut pattern_code = self.code_at(pattern_location).to_string();
7838+
let mut pattern_code = code_at(self.module, pattern_location).to_string();
78327839
if !references_to_matched_variable.is_empty() {
78337840
pattern_code = format!("{pattern_code} as {}", matched_variable.name());
78347841
};
78357842
pattern_with_variable(pattern_code)
78367843
})
78377844
.join(" | ");
78387845

7839-
let clause_code = self.code_at(clause.then.location());
7846+
let clause_code = code_at(self.module, clause.then.location());
78407847
let guard_code = match (outer_guard, &clause.guard) {
78417848
(Some(outer), Some(inner)) => {
7842-
let mut outer_code = self.code_at(outer.location()).to_string();
7843-
let mut inner_code = self.code_at(inner.location()).to_string();
7849+
let mut outer_code = code_at(self.module, outer.location()).to_string();
7850+
let mut inner_code = code_at(self.module, inner.location()).to_string();
78447851
if ast::BinOp::And.precedence() > outer.precedence() {
78457852
outer_code = format!("{{ {outer_code} }}")
78467853
}
@@ -7850,7 +7857,7 @@ impl<'a> CollapseNestedCase<'a> {
78507857
format!(" if {outer_code} && {inner_code}")
78517858
}
78527859
(None, Some(guard)) | (Some(guard), None) => {
7853-
format!(" if {}", self.code_at(guard.location()))
7860+
format!(" if {}", code_at(self.module, guard.location()))
78547861
}
78557862
(None, None) => "".into(),
78567863
};
@@ -7879,13 +7886,6 @@ impl<'a> CollapseNestedCase<'a> {
78797886
action
78807887
}
78817888

7882-
fn code_at(&self, span: SrcSpan) -> &str {
7883-
self.module
7884-
.code
7885-
.get(span.start as usize..span.end as usize)
7886-
.expect("location must be valid")
7887-
}
7888-
78897889
/// If the clause can be flattened because it's matching on a single variable
78907890
/// defined in it, this function will return the info needed by the language
78917891
/// server to flatten that case.

0 commit comments

Comments
 (0)