@@ -14,20 +14,24 @@ use crate::{
14
14
config:: PackageConfig ,
15
15
exhaustiveness:: CompiledCase ,
16
16
io:: { BeamCompiler , CommandExecutor , FileSystemReader , FileSystemWriter } ,
17
- language_server:: { edits, reference:: FindVariableReferences } ,
17
+ language_server:: { edits, lsp_range_to_src_span , reference:: FindVariableReferences } ,
18
18
line_numbers:: LineNumbers ,
19
19
parse:: { extra:: ModuleExtra , lexer:: str_to_keyword} ,
20
20
strings:: to_snake_case,
21
21
type_:: {
22
- self , FieldMap , ModuleValueConstructor , Type , TypeVar , TypedCallArg , ValueConstructor ,
22
+ self , Error as TypeError , FieldMap , ModuleValueConstructor , Type , TypeVar , TypedCallArg ,
23
+ ValueConstructor ,
23
24
error:: { ModuleSuggestion , VariableDeclaration , VariableOrigin } ,
24
25
printer:: Printer ,
25
26
} ,
26
27
} ;
27
28
use ecow:: { EcoString , eco_format} ;
28
29
use im:: HashMap ;
29
30
use itertools:: Itertools ;
30
- use lsp_types:: { CodeAction , CodeActionKind , CodeActionParams , Position , Range , TextEdit , Url } ;
31
+ use lsp_types:: {
32
+ CodeAction , CodeActionKind , CodeActionParams , CreateFile , CreateFileOptions ,
33
+ DocumentChangeOperation , DocumentChanges , Position , Range , ResourceOp , TextEdit , Url ,
34
+ } ;
31
35
use vec1:: { Vec1 , vec1} ;
32
36
33
37
use super :: {
@@ -46,7 +50,7 @@ pub struct CodeActionBuilder {
46
50
}
47
51
48
52
impl CodeActionBuilder {
49
- pub fn new ( title : & str ) -> Self {
53
+ pub fn new ( title : impl ToString ) -> Self {
50
54
Self {
51
55
action : CodeAction {
52
56
title : title. to_string ( ) ,
@@ -76,6 +80,15 @@ impl CodeActionBuilder {
76
80
self
77
81
}
78
82
83
+ pub fn document_changes ( mut self , changes : DocumentChanges ) -> Self {
84
+ let mut edit = self . action . edit . take ( ) . unwrap_or_default ( ) ;
85
+
86
+ edit. document_changes = Some ( changes) ;
87
+
88
+ self . action . edit = Some ( edit) ;
89
+ self
90
+ }
91
+
79
92
pub fn preferred ( mut self , is_preferred : bool ) -> Self {
80
93
self . action . is_preferred = Some ( is_preferred) ;
81
94
self
@@ -7895,3 +7908,91 @@ fn single_expression(expression: &TypedExpr) -> Option<&TypedExpr> {
7895
7908
expression => Some ( expression) ,
7896
7909
}
7897
7910
}
7911
+
7912
+ /// Code action to create an unknown module
7913
+ ///
7914
+ /// ```gleam
7915
+ /// // foo.gleam
7916
+ /// // Diagnostic: Unknown module
7917
+ /// import foo/bar/baz
7918
+ /// ```
7919
+ ///
7920
+ /// Would create:
7921
+ ///
7922
+ /// ```gleam
7923
+ /// // foo/bar/baz.gleam
7924
+ /// ```
7925
+ ///
7926
+ pub struct CreateUnknownModule < ' a > {
7927
+ module : & ' a Module ,
7928
+ error : & ' a Option < Error > ,
7929
+ code_action_span : SrcSpan ,
7930
+ }
7931
+
7932
+ impl < ' a > CreateUnknownModule < ' a > {
7933
+ pub fn new (
7934
+ module : & ' a Module ,
7935
+ lines : & ' a LineNumbers ,
7936
+ params : & ' a CodeActionParams ,
7937
+ error : & ' a Option < Error > ,
7938
+ ) -> Self {
7939
+ Self {
7940
+ module,
7941
+ error,
7942
+ code_action_span : lsp_range_to_src_span ( params. range , lines) ,
7943
+ }
7944
+ }
7945
+
7946
+ pub fn code_actions ( self ) -> Vec < CodeAction > {
7947
+ struct UnknownModule < ' a > {
7948
+ name : & ' a EcoString ,
7949
+ location : & ' a SrcSpan ,
7950
+ }
7951
+
7952
+ let mut actions = vec ! [ ] ;
7953
+
7954
+ let Some ( Error :: Type { errors, .. } ) = self . error else {
7955
+ return actions;
7956
+ } ;
7957
+
7958
+ let origin_path = self
7959
+ . module
7960
+ . input_path
7961
+ . as_str ( )
7962
+ . strip_suffix ( & format ! ( "{}.gleam" , self . module. name) )
7963
+ . expect ( "origin is ancestor of module path" ) ;
7964
+
7965
+ let unknown_modules = errors. iter ( ) . filter_map ( |error| {
7966
+ if let TypeError :: UnknownModule { name, location, .. } = error {
7967
+ return Some ( UnknownModule { name, location } ) ;
7968
+ }
7969
+
7970
+ None
7971
+ } ) ;
7972
+
7973
+ for unknown_module in unknown_modules {
7974
+ if !self . code_action_span . intersects ( * unknown_module. location ) {
7975
+ continue ;
7976
+ }
7977
+
7978
+ let uri = Url :: from_file_path ( format ! ( "{origin_path}/{}.gleam" , unknown_module. name) )
7979
+ . expect ( "origin path is absolute" ) ;
7980
+
7981
+ CodeActionBuilder :: new ( format ! ( "Create module {}.gleam" , unknown_module. name) )
7982
+ . kind ( CodeActionKind :: QUICKFIX )
7983
+ . document_changes ( DocumentChanges :: Operations ( vec ! [
7984
+ DocumentChangeOperation :: Op ( ResourceOp :: Create ( CreateFile {
7985
+ uri,
7986
+ options: Some ( CreateFileOptions {
7987
+ overwrite: Some ( false ) ,
7988
+ ignore_if_exists: Some ( true ) ,
7989
+ } ) ,
7990
+ annotation_id: None ,
7991
+ } ) ) ,
7992
+ ] ) )
7993
+ . push_to ( & mut actions) ;
7994
+ }
7995
+
7996
+ actions
7997
+ }
7998
+ }
0 commit comments