sway_lsp/capabilities/
runnable.rs

1use 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    /// The location in the file where the runnable button should be displayed
8    pub range: Range,
9    /// The program kind of the current file
10    pub tree_type: TreeType,
11}
12
13#[derive(Debug, Eq, PartialEq, Clone)]
14pub struct RunnableTestFn {
15    /// The location in the file where the runnable button should be displayed
16    pub range: Range,
17    /// Additional arguments to use with the runnable command.
18    pub test_name: Option<String>,
19}
20
21/// A runnable is a sway function that can be executed in the editor.
22pub trait Runnable: core::fmt::Debug + Send + Sync + 'static {
23    /// The command to execute.
24    fn command(&self) -> Command {
25        Command {
26            command: self.cmd_string(),
27            title: self.label_string(),
28            arguments: self.arguments(),
29        }
30    }
31    /// The command name defined in the client.
32    fn cmd_string(&self) -> String;
33    /// The label to display in the editor.
34    fn label_string(&self) -> String;
35    /// The arguments to pass to the command.
36    fn arguments(&self) -> Option<Vec<Value>>;
37    /// The range in the file where the runnable button should be displayed.
38    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}