sway_lsp/capabilities/
runnable.rs1use lsp_types::{Command, Range};
2use serde_json::{json, Value};
3use sway_core::language::parsed::TreeType;
4
5#[derive(Debug, Eq, PartialEq, Clone)]
6pub struct RunnableMainFn {
7 pub range: Range,
9 pub tree_type: TreeType,
11}
12
13#[derive(Debug, Eq, PartialEq, Clone)]
14pub struct RunnableTestFn {
15 pub range: Range,
17 pub test_name: Option<String>,
19}
20
21pub trait Runnable: core::fmt::Debug + Send + Sync + 'static {
23 fn command(&self) -> Command {
25 Command {
26 command: self.cmd_string(),
27 title: self.label_string(),
28 arguments: self.arguments(),
29 }
30 }
31 fn cmd_string(&self) -> String;
33 fn label_string(&self) -> String;
35 fn arguments(&self) -> Option<Vec<Value>>;
37 fn range(&self) -> &Range;
39}
40
41impl Runnable for RunnableMainFn {
42 fn cmd_string(&self) -> String {
43 "sway.runScript".to_string()
44 }
45 fn label_string(&self) -> String {
46 "▶\u{fe0e} Run".to_string()
47 }
48 fn arguments(&self) -> Option<Vec<Value>> {
49 None
50 }
51 fn range(&self) -> &Range {
52 &self.range
53 }
54}
55
56impl Runnable for RunnableTestFn {
57 fn cmd_string(&self) -> String {
58 "sway.runTests".to_string()
59 }
60 fn label_string(&self) -> String {
61 "▶\u{fe0e} Run Test".to_string()
62 }
63 fn arguments(&self) -> Option<Vec<Value>> {
64 self.test_name
65 .as_ref()
66 .map(|test_name| vec![json!({ "name": test_name })])
67 }
68 fn range(&self) -> &Range {
69 &self.range
70 }
71}