1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
pub mod abi_decl;
pub mod struct_decl;

use crate::core::{
    session::Session,
    token::{Token, TypedAstToken},
    token_map::TokenMap,
};
pub use crate::error::DocumentError;
use serde_json::Value;
use std::{collections::HashMap, sync::Arc};
use sway_core::{
    language::ty::TyDecl,
    transform::{AttributeKind, AttributesMap},
    Engines, TypeParameter,
};
use sway_types::Spanned;
use tower_lsp::lsp_types::{
    CodeAction as LspCodeAction, CodeActionDisabled, CodeActionKind, CodeActionOrCommand,
    CodeActionResponse, Position, Range, TextDocumentIdentifier, TextEdit, Url, WorkspaceEdit,
};

pub(crate) const CODE_ACTION_IMPL_TITLE: &str = "Generate impl for";
pub(crate) const CODE_ACTION_NEW_TITLE: &str = "Generate `new`";
pub(crate) const CONTRACT: &str = "Contract";
pub(crate) const TAB: &str = "    ";

#[derive(Clone)]
pub(crate) struct CodeActionContext<'a> {
    engines: Engines<'a>,
    tokens: &'a TokenMap,
    token: &'a Token,
    uri: &'a Url,
}

pub(crate) fn code_actions(
    session: Arc<Session>,
    range: &Range,
    text_document: TextDocumentIdentifier,
    temp_uri: &Url,
) -> Option<CodeActionResponse> {
    let (_, token) = session
        .token_map()
        .token_at_position(temp_uri, range.start)?;
    let type_engine = session.type_engine.read();
    let decl_engine = session.decl_engine.read();
    let ctx = CodeActionContext {
        engines: Engines::new(&type_engine, &decl_engine),
        tokens: session.token_map(),
        token: &token.clone(),
        uri: &text_document.uri,
    };
    token.typed.and_then(|typed_token| match typed_token {
        TypedAstToken::TypedDeclaration(decl) => match decl {
            TyDecl::AbiDecl { decl_id, .. } => abi_decl::code_actions(&decl_id, ctx),
            TyDecl::StructDecl { decl_id, .. } => struct_decl::code_actions(&decl_id, ctx),
            _ => None,
        },
        _ => None,
    })
}

pub(crate) trait CodeAction<'a, T: Spanned> {
    /// Creates a new [CodeAction] with the given [Engines], delcaration type, and [Url].
    fn new(ctx: CodeActionContext<'a>, decl: &'a T) -> Self;

    /// Returns a [String] of text to insert into the document.
    fn new_text(&self) -> String;

    /// Returns a [String] of text to use as the title of the code action.
    fn title(&self) -> String;

    /// Returns a [String] hold the name of the declaration.
    fn decl_name(&self) -> String;

    /// Returns the declaration.
    fn decl(&self) -> &T;

    /// Returns the declaration's [Url].
    fn uri(&self) -> &Url;

    /// Returns an optional [CodeActionDisabled] indicating whether this code action should be disabled.
    fn disabled(&self) -> Option<CodeActionDisabled> {
        None
    }

    /// Returns a [CodeActionOrCommand] for the given code action.
    fn code_action(&self) -> CodeActionOrCommand {
        let text_edit = TextEdit {
            range: self.range(),
            new_text: self.new_text(),
        };
        let changes = HashMap::from([(self.uri().clone(), vec![text_edit])]);

        CodeActionOrCommand::CodeAction(LspCodeAction {
            title: self.title(),
            kind: Some(CodeActionKind::REFACTOR),
            edit: Some(WorkspaceEdit {
                changes: Some(changes),
                ..Default::default()
            }),
            data: Some(Value::String(self.uri().to_string())),
            disabled: self.disabled(),
            ..Default::default()
        })
    }

    /// Returns the [Range] to insert text after the last line of the span, with an empty line in between.
    /// Can be overridden if the code action calls for it.
    fn range(&self) -> Range {
        let (last_line, _) = self.decl().span().end_pos().line_col();
        let insertion_position = Position {
            line: last_line as u32,
            character: 0,
        };
        Range {
            start: insertion_position,
            end: insertion_position,
        }
    }

    /// Returns an optional [String] of the type parameters for the given [TypeParameter] vector.
    fn type_param_string(&self, type_params: &Vec<TypeParameter>) -> Option<String> {
        if type_params.is_empty() {
            None
        } else {
            Some(
                type_params
                    .iter()
                    .map(|param| param.name_ident.to_string())
                    .collect::<Vec<_>>()
                    .join(", "),
            )
        }
    }

    /// Returns a [String] of a generated impl with the optional `for <for_name>` signature.
    /// Can be used for both ABI and Struct impls.
    fn impl_string(
        &self,
        type_params: Option<String>,
        body: String,
        for_name: Option<String>,
    ) -> String {
        let for_string = match for_name {
            Some(name) => format!(" for {name}"),
            None => "".to_string(),
        };
        let type_param_string = match type_params {
            Some(params) => format!("<{params}>"),
            None => "".to_string(),
        };
        format!(
            "\nimpl{} {}{}{} {{{}}}\n",
            type_param_string,
            self.decl_name(),
            type_param_string,
            for_string,
            body
        )
    }

    /// Returns a [String] of a an attribute map, optionally excluding comments.
    fn attribute_string(&self, attr_map: &AttributesMap, include_comments: bool) -> String {
        let attr_string = attr_map
            .iter()
            .map(|(kind, attrs)| {
                attrs
                    .iter()
                    .filter_map(|attr| match kind {
                        AttributeKind::DocComment { .. } => {
                            if include_comments {
                                return Some(format!("{}{}", TAB, attr.span.as_str()));
                            }
                            None
                        }
                        _ => Some(format!("{}{}", TAB, attr.span.as_str())),
                    })
                    .collect::<Vec<String>>()
                    .join("\n")
            })
            .collect::<Vec<String>>()
            .join("\n");
        let attribute_padding = match attr_string.len() > 1 {
            true => "\n",
            false => "",
        };
        format!("{attr_string}{attribute_padding}")
    }

    /// Returns a [String] of a generated function signature.
    fn fn_signature_string(
        &self,
        fn_name: String,
        params_string: String,
        attr_map: &AttributesMap,
        return_type_string: String,
        body: Option<String>,
    ) -> String {
        let attribute_string = self.attribute_string(attr_map, false);
        let body_string = match body {
            Some(body) => format!(" {body} "),
            None => String::new(),
        };
        format!(
            "{attribute_string}{TAB}fn {fn_name}({params_string}){return_type_string} {{{body_string}}}",
        )
    }
}