@@ -10,24 +10,29 @@ use crate::{
10
10
TypedExpr , TypedModuleConstant , TypedPattern , TypedPipelineAssignment ,
11
11
TypedRecordConstructor , TypedStatement , TypedUse , visit:: Visit as _,
12
12
} ,
13
- build:: { Located , Module } ,
13
+ build:: { Located , Module , Origin } ,
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
+ paths:: ProjectPaths ,
20
21
strings:: to_snake_case,
21
22
type_:: {
22
- self , FieldMap , ModuleValueConstructor , Type , TypeVar , TypedCallArg , ValueConstructor ,
23
+ self , Error as TypeError , FieldMap , ModuleValueConstructor , Type , TypeVar , TypedCallArg ,
24
+ ValueConstructor ,
23
25
error:: { ModuleSuggestion , VariableDeclaration , VariableOrigin } ,
24
26
printer:: Printer ,
25
27
} ,
26
28
} ;
27
29
use ecow:: { EcoString , eco_format} ;
28
30
use im:: HashMap ;
29
31
use itertools:: Itertools ;
30
- use lsp_types:: { CodeAction , CodeActionKind , CodeActionParams , Position , Range , TextEdit , Url } ;
32
+ use lsp_types:: {
33
+ CodeAction , CodeActionKind , CodeActionParams , CreateFile , CreateFileOptions ,
34
+ DocumentChangeOperation , DocumentChanges , Position , Range , ResourceOp , TextEdit , Url ,
35
+ } ;
31
36
use vec1:: { Vec1 , vec1} ;
32
37
33
38
use super :: {
@@ -46,7 +51,7 @@ pub struct CodeActionBuilder {
46
51
}
47
52
48
53
impl CodeActionBuilder {
49
- pub fn new ( title : & str ) -> Self {
54
+ pub fn new ( title : impl ToString ) -> Self {
50
55
Self {
51
56
action : CodeAction {
52
57
title : title. to_string ( ) ,
@@ -76,6 +81,15 @@ impl CodeActionBuilder {
76
81
self
77
82
}
78
83
84
+ pub fn document_changes ( mut self , changes : DocumentChanges ) -> Self {
85
+ let mut edit = self . action . edit . take ( ) . unwrap_or_default ( ) ;
86
+
87
+ edit. document_changes = Some ( changes) ;
88
+
89
+ self . action . edit = Some ( edit) ;
90
+ self
91
+ }
92
+
79
93
pub fn preferred ( mut self , is_preferred : bool ) -> Self {
80
94
self . action . is_preferred = Some ( is_preferred) ;
81
95
self
@@ -1571,7 +1585,7 @@ impl<'a> QualifiedToUnqualifiedImportSecondPass<'a> {
1571
1585
}
1572
1586
self . edit_import ( ) ;
1573
1587
let mut action = Vec :: with_capacity ( 1 ) ;
1574
- CodeActionBuilder :: new ( & format ! (
1588
+ CodeActionBuilder :: new ( format ! (
1575
1589
"Unqualify {}.{}" ,
1576
1590
self . qualified_constructor. used_name, self . qualified_constructor. constructor
1577
1591
) )
@@ -1959,7 +1973,7 @@ impl<'a> UnqualifiedToQualifiedImportSecondPass<'a> {
1959
1973
constructor,
1960
1974
..
1961
1975
} = self . unqualified_constructor ;
1962
- CodeActionBuilder :: new ( & format ! (
1976
+ CodeActionBuilder :: new ( format ! (
1963
1977
"Qualify {} as {}.{}" ,
1964
1978
constructor. used_name( ) ,
1965
1979
module_name,
@@ -7081,7 +7095,7 @@ impl<'a> FixBinaryOperation<'a> {
7081
7095
self . edits . replace ( location, replacement. name ( ) . into ( ) ) ;
7082
7096
7083
7097
let mut action = Vec :: with_capacity ( 1 ) ;
7084
- CodeActionBuilder :: new ( & format ! ( "Use `{}`" , replacement. name( ) ) )
7098
+ CodeActionBuilder :: new ( format ! ( "Use `{}`" , replacement. name( ) ) )
7085
7099
. kind ( CodeActionKind :: REFACTOR_REWRITE )
7086
7100
. changes ( self . params . text_document . uri . clone ( ) , self . edits . edits )
7087
7101
. preferred ( true )
@@ -7164,7 +7178,7 @@ impl<'a> FixTruncatedBitArraySegment<'a> {
7164
7178
. replace ( truncation. value_location , replacement. clone ( ) ) ;
7165
7179
7166
7180
let mut action = Vec :: with_capacity ( 1 ) ;
7167
- CodeActionBuilder :: new ( & format ! ( "Replace with `{replacement}`" ) )
7181
+ CodeActionBuilder :: new ( format ! ( "Replace with `{replacement}`" ) )
7168
7182
. kind ( CodeActionKind :: REFACTOR_REWRITE )
7169
7183
. changes ( self . params . text_document . uri . clone ( ) , self . edits . edits )
7170
7184
. preferred ( true )
@@ -8006,3 +8020,105 @@ fn single_expression(expression: &TypedExpr) -> Option<&TypedExpr> {
8006
8020
expression => Some ( expression) ,
8007
8021
}
8008
8022
}
8023
+
8024
+ /// Code action to create unknown modules when an import is added for a
8025
+ /// module that doesn't exist.
8026
+ ///
8027
+ /// For example, if `import wobble/woo` is added to `src/wiggle.gleam`,
8028
+ /// then a code action to create `src/wobble/woo.gleam` will be presented
8029
+ /// when triggered over `import wobble/woo`.
8030
+ pub struct CreateUnknownModule < ' a > {
8031
+ module : & ' a Module ,
8032
+ lines : & ' a LineNumbers ,
8033
+ params : & ' a CodeActionParams ,
8034
+ paths : & ' a ProjectPaths ,
8035
+ error : & ' a Option < Error > ,
8036
+ }
8037
+
8038
+ impl < ' a > CreateUnknownModule < ' a > {
8039
+ pub fn new (
8040
+ module : & ' a Module ,
8041
+ lines : & ' a LineNumbers ,
8042
+ params : & ' a CodeActionParams ,
8043
+ paths : & ' a ProjectPaths ,
8044
+ error : & ' a Option < Error > ,
8045
+ ) -> Self {
8046
+ Self {
8047
+ module,
8048
+ lines,
8049
+ params,
8050
+ paths,
8051
+ error,
8052
+ }
8053
+ }
8054
+
8055
+ pub fn code_actions ( self ) -> Vec < CodeAction > {
8056
+ struct UnknownModule < ' a > {
8057
+ name : & ' a EcoString ,
8058
+ location : & ' a SrcSpan ,
8059
+ }
8060
+
8061
+ let mut actions = vec ! [ ] ;
8062
+
8063
+ // This code action can be derived from UnknownModule type errors. If those
8064
+ // errors don't exist, there are no actions to add.
8065
+ let Some ( Error :: Type { errors, .. } ) = self . error else {
8066
+ return actions;
8067
+ } ;
8068
+
8069
+ // Span of the code action so we can check if it exists within the span of
8070
+ // the UnkownModule type error
8071
+ let code_action_span = lsp_range_to_src_span ( self . params . range , self . lines ) ;
8072
+
8073
+ // Origin directory we can build the new module path from
8074
+ let origin_directory = match self . module . origin {
8075
+ Origin :: Src => self . paths . src_directory ( ) ,
8076
+ Origin :: Test => self . paths . test_directory ( ) ,
8077
+ Origin :: Dev => self . paths . dev_directory ( ) ,
8078
+ } ;
8079
+
8080
+ // Filter for any UnknownModule type errors
8081
+ let unknown_modules = errors. iter ( ) . filter_map ( |error| {
8082
+ if let TypeError :: UnknownModule { name, location, .. } = error {
8083
+ return Some ( UnknownModule { name, location } ) ;
8084
+ }
8085
+
8086
+ None
8087
+ } ) ;
8088
+
8089
+ // For each UnknownModule type error, check to see if it contains the
8090
+ // incoming code action & if so, add a document change to create the module
8091
+ for unknown_module in unknown_modules {
8092
+ // Was this code action triggered within the UnknownModule error?
8093
+ let error_contains_action = unknown_module. location . contains ( code_action_span. start )
8094
+ && unknown_module. location . contains ( code_action_span. end ) ;
8095
+
8096
+ if !error_contains_action {
8097
+ continue ;
8098
+ }
8099
+
8100
+ let uri = url_from_path ( & format ! ( "{origin_directory}/{}.gleam" , unknown_module. name) )
8101
+ . expect ( "origin directory is absolute" ) ;
8102
+
8103
+ CodeActionBuilder :: new ( format ! (
8104
+ "Create {}/{}.gleam" ,
8105
+ self . module. origin. folder_name( ) ,
8106
+ unknown_module. name
8107
+ ) )
8108
+ . kind ( CodeActionKind :: QUICKFIX )
8109
+ . document_changes ( DocumentChanges :: Operations ( vec ! [
8110
+ DocumentChangeOperation :: Op ( ResourceOp :: Create ( CreateFile {
8111
+ uri,
8112
+ options: Some ( CreateFileOptions {
8113
+ overwrite: Some ( false ) ,
8114
+ ignore_if_exists: Some ( true ) ,
8115
+ } ) ,
8116
+ annotation_id: None ,
8117
+ } ) ) ,
8118
+ ] ) )
8119
+ . push_to ( & mut actions) ;
8120
+ }
8121
+
8122
+ actions
8123
+ }
8124
+ }
0 commit comments