Skip to content

Commit 8a33613

Browse files
committed
feat: receive the range in the command
1 parent 6750527 commit 8a33613

File tree

3 files changed

+42
-49
lines changed

3 files changed

+42
-49
lines changed

src/app/commands.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where
7070
pub async fn scan_image(
7171
&self,
7272
uri: &str,
73-
line: u32,
73+
range: Range,
7474
image_name: &str,
7575
image_scanner: &impl ImageScanner,
7676
) -> Result<()> {
@@ -92,28 +92,8 @@ where
9292
.await;
9393

9494
let diagnostic = {
95-
let document_text = self
96-
.document_database
97-
.read_document_text(uri)
98-
.await
99-
.ok_or_else(|| {
100-
Error::internal_error().with_message("unable to obtain document to scan")
101-
})?;
102-
103-
let range_for_selected_line = Range::new(
104-
Position::new(line, 0),
105-
Position::new(
106-
line,
107-
document_text
108-
.lines()
109-
.nth(line as usize)
110-
.map(|x| x.len() as u32)
111-
.unwrap_or(u32::MAX),
112-
),
113-
);
114-
11595
let mut diagnostic = Diagnostic {
116-
range: range_for_selected_line,
96+
range,
11797
severity: Some(DiagnosticSeverity::HINT),
11898
message: "No vulnerabilities found.".to_owned(),
11999
..Default::default()

src/app/lsp_server.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tower_lsp::lsp_types::{
1111
CodeLens, CodeLensOptions, CodeLensParams, Command, DidChangeConfigurationParams,
1212
DidChangeTextDocumentParams, DidOpenTextDocumentParams, ExecuteCommandOptions,
1313
ExecuteCommandParams, InitializeParams, InitializeResult, InitializedParams, MessageType,
14-
Position, Range, ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind,
14+
Range, ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind,
1515
};
1616
use tracing::{debug, info};
1717

@@ -126,7 +126,7 @@ where
126126
command: SupportedCommands::ExecuteBaseImageScan.to_string(),
127127
arguments: Some(vec![
128128
json!(uri),
129-
json!(instruction.range.start.line),
129+
json!(instruction.range),
130130
json!(instruction.image_name),
131131
]),
132132
range: instruction.range,
@@ -148,10 +148,7 @@ where
148148
.filter(|instruction| instruction.keyword == "FROM")
149149
.next_back()
150150
{
151-
let range = Range::new(
152-
Position::new(last_from_instruction.range.start.line, 0),
153-
Position::new(last_from_instruction.range.start.line, 0),
154-
);
151+
let range = last_from_instruction.range;
155152
let line = last_from_instruction.range.start.line;
156153
commands.push(CommandInfo {
157154
title: "Build and scan".to_string(),
@@ -164,7 +161,7 @@ where
164161
commands.push(CommandInfo {
165162
title: "Scan base image".to_string(),
166163
command: SupportedCommands::ExecuteBaseImageScan.to_string(),
167-
arguments: Some(vec![json!(uri), json!(line), json!(image_name)]),
164+
arguments: Some(vec![json!(uri), json!(range), json!(image_name)]),
168165
range,
169166
});
170167
}
@@ -344,12 +341,12 @@ async fn execute_command_scan_base_image<C: LSPClient>(
344341
return Err(Error::internal_error().with_message("uri is not a string"));
345342
};
346343

347-
let Some(line) = params.arguments.get(1) else {
348-
return Err(Error::internal_error().with_message("no line was provided"));
344+
let Some(range) = params.arguments.get(1) else {
345+
return Err(Error::internal_error().with_message("no range was provided"));
349346
};
350347

351-
let Some(line) = line.as_u64().and_then(|x| u32::try_from(x).ok()) else {
352-
return Err(Error::internal_error().with_message("line is not a u32"));
348+
let Ok(range) = serde_json::from_value::<Range>(range.clone()) else {
349+
return Err(Error::internal_error().with_message("range is not a Range object"));
353350
};
354351

355352
let Some(image_name) = params.arguments.get(2) else {
@@ -369,7 +366,7 @@ async fn execute_command_scan_base_image<C: LSPClient>(
369366

370367
server
371368
.command_executor
372-
.scan_image(uri, line, image_name, &image_scanner)
369+
.scan_image(uri, range, image_name, &image_scanner)
373370
.await?;
374371

375372
Ok(())

tests/general.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ async fn when_the_client_asks_for_the_existing_code_actions_it_receives_the_avai
4242
CodeActionOrCommand::Command(Command {
4343
title: "Scan base image".to_string(),
4444
command: "sysdig-lsp.execute-scan".to_string(),
45-
arguments: Some(vec![json!("file://dockerfile/"), json!(0), json!("alpine")])
45+
arguments: Some(vec![
46+
json!("file://dockerfile/"),
47+
json!({"start": {"line": 0, "character": 0}, "end": {"line": 0, "character": 11}}),
48+
json!("alpine"),
49+
])
4650
})
4751
]
4852
);
@@ -77,7 +81,11 @@ async fn when_the_client_asks_for_the_existing_code_actions_but_the_dockerfile_c
7781
CodeActionOrCommand::Command(Command {
7882
title: "Scan base image".to_string(),
7983
command: "sysdig-lsp.execute-scan".to_string(),
80-
arguments: Some(vec![json!("file://dockerfile/"), json!(1), json!("ubuntu")])
84+
arguments: Some(vec![
85+
json!("file://dockerfile/"),
86+
json!({"start": {"line": 1, "character": 0}, "end": {"line": 1, "character": 11}}),
87+
json!("ubuntu"),
88+
])
8189
})
8290
]
8391
);
@@ -102,7 +110,7 @@ async fn when_the_client_asks_for_the_existing_code_lens_it_receives_the_availab
102110
response.unwrap(),
103111
vec![
104112
CodeLens {
105-
range: Range::new(Position::new(0, 0), Position::new(0, 0)),
113+
range: Range::new(Position::new(0, 0), Position::new(0, 11)),
106114
command: Some(Command {
107115
title: "Build and scan".to_string(),
108116
command: "sysdig-lsp.execute-build-and-scan".to_string(),
@@ -111,11 +119,15 @@ async fn when_the_client_asks_for_the_existing_code_lens_it_receives_the_availab
111119
data: None
112120
},
113121
CodeLens {
114-
range: Range::new(Position::new(0, 0), Position::new(0, 0)),
122+
range: Range::new(Position::new(0, 0), Position::new(0, 11)),
115123
command: Some(Command {
116124
title: "Scan base image".to_string(),
117125
command: "sysdig-lsp.execute-scan".to_string(),
118-
arguments: Some(vec![json!("file://dockerfile/"), json!(0), json!("alpine")])
126+
arguments: Some(vec![
127+
json!("file://dockerfile/"),
128+
json!({"start": {"line": 0, "character": 0}, "end": {"line": 0, "character": 11}}),
129+
json!("alpine"),
130+
])
119131
}),
120132
data: None
121133
}
@@ -139,7 +151,7 @@ async fn when_the_client_asks_for_the_existing_code_lens_but_the_dockerfile_cont
139151
response.unwrap(),
140152
vec![
141153
CodeLens {
142-
range: Range::new(Position::new(1, 0), Position::new(1, 0)),
154+
range: Range::new(Position::new(1, 0), Position::new(1, 11)),
143155
command: Some(Command {
144156
title: "Build and scan".to_string(),
145157
command: "sysdig-lsp.execute-build-and-scan".to_string(),
@@ -148,11 +160,15 @@ async fn when_the_client_asks_for_the_existing_code_lens_but_the_dockerfile_cont
148160
data: None
149161
},
150162
CodeLens {
151-
range: Range::new(Position::new(1, 0), Position::new(1, 0)),
163+
range: Range::new(Position::new(1, 0), Position::new(1, 11)),
152164
command: Some(Command {
153165
title: "Scan base image".to_string(),
154166
command: "sysdig-lsp.execute-scan".to_string(),
155-
arguments: Some(vec![json!("file://dockerfile/"), json!(1), json!("ubuntu")])
167+
arguments: Some(vec![
168+
json!("file://dockerfile/"),
169+
json!({"start": {"line": 1, "character": 0}, "end": {"line": 1, "character": 11}}),
170+
json!("ubuntu"),
171+
])
156172
}),
157173
data: None
158174
}
@@ -184,7 +200,7 @@ async fn when_the_client_asks_for_code_lens_in_a_compose_file_it_receives_them()
184200
command: "sysdig-lsp.execute-scan".to_string(),
185201
arguments: Some(vec![
186202
json!("file://docker-compose.yml/"),
187-
json!(2),
203+
json!({"start": {"line": 2, "character": 11}, "end": {"line": 2, "character": 23}}),
188204
json!("nginx:latest")
189205
])
190206
}),
@@ -197,7 +213,7 @@ async fn when_the_client_asks_for_code_lens_in_a_compose_file_it_receives_them()
197213
command: "sysdig-lsp.execute-scan".to_string(),
198214
arguments: Some(vec![
199215
json!("file://docker-compose.yml/"),
200-
json!(4),
216+
json!({"start": {"line": 4, "character": 11}, "end": {"line": 4, "character": 22}}),
201217
json!("postgres:13")
202218
])
203219
}),
@@ -228,8 +244,8 @@ async fn when_the_client_asks_for_code_actions_in_a_compose_file_it_receives_the
228244
command: "sysdig-lsp.execute-scan".to_string(),
229245
arguments: Some(vec![
230246
json!("file://docker-compose.yml/"),
231-
json!(2),
232-
json!("nginx:latest")
247+
json!({"start": {"line": 2, "character": 11}, "end": {"line": 2, "character": 23}}),
248+
json!("nginx:latest"),
233249
])
234250
})]
235251
);
@@ -256,7 +272,7 @@ async fn when_the_client_asks_for_code_lens_in_a_complex_compose_yaml_file_it_re
256272
command: "sysdig-lsp.execute-scan".to_string(),
257273
arguments: Some(vec![
258274
json!("file://compose.yaml/"),
259-
json!(4),
275+
json!({"start": {"line": 4, "character": 13}, "end": {"line": 4, "character": 25}}),
260276
json!("nginx:latest")
261277
])
262278
}),
@@ -269,7 +285,7 @@ async fn when_the_client_asks_for_code_lens_in_a_complex_compose_yaml_file_it_re
269285
command: "sysdig-lsp.execute-scan".to_string(),
270286
arguments: Some(vec![
271287
json!("file://compose.yaml/"),
272-
json!(9),
288+
json!({"start": {"line": 9, "character": 6}, "end": {"line": 9, "character": 17}}),
273289
json!("postgres:13")
274290
])
275291
}),
@@ -282,7 +298,7 @@ async fn when_the_client_asks_for_code_lens_in_a_complex_compose_yaml_file_it_re
282298
command: "sysdig-lsp.execute-scan".to_string(),
283299
arguments: Some(vec![
284300
json!("file://compose.yaml/"),
285-
json!(13),
301+
json!({"start": {"line": 13, "character": 11}, "end": {"line": 13, "character": 21}}),
286302
json!("my-api:1.0")
287303
])
288304
}),

0 commit comments

Comments
 (0)