@@ -2,9 +2,9 @@ use std::num::NonZeroUsize;
22
33use miniscript:: iter:: TreeLike ;
44
5+ use crate :: error:: LspError ;
56use ropey:: Rope ;
67use simplicityhl:: parse;
7- use tower_lsp_server:: jsonrpc:: { Error , Result } ;
88use tower_lsp_server:: lsp_types;
99
1010fn position_le ( a : & simplicityhl:: error:: Position , b : & simplicityhl:: error:: Position ) -> bool {
@@ -26,15 +26,11 @@ pub fn span_contains(a: &simplicityhl::error::Span, b: &simplicityhl::error::Spa
2626/// `Position` required for diagnostic starts with zero
2727pub fn span_to_positions (
2828 span : & simplicityhl:: error:: Span ,
29- ) -> Result < ( lsp_types:: Position , lsp_types:: Position ) > {
30- let start_line = u32:: try_from ( span. start . line . get ( ) )
31- . map_err ( |e| Error :: invalid_params ( format ! ( "line overflow: {e}" ) ) ) ?;
32- let start_col = u32:: try_from ( span. start . col . get ( ) )
33- . map_err ( |e| Error :: invalid_params ( format ! ( "col overflow: {e}" ) ) ) ?;
34- let end_line = u32:: try_from ( span. end . line . get ( ) )
35- . map_err ( |e| Error :: invalid_params ( format ! ( "line overflow: {e}" ) ) ) ?;
36- let end_col = u32:: try_from ( span. end . col . get ( ) )
37- . map_err ( |e| Error :: invalid_params ( format ! ( "col overflow: {e}" ) ) ) ?;
29+ ) -> Result < ( lsp_types:: Position , lsp_types:: Position ) , LspError > {
30+ let start_line = u32:: try_from ( span. start . line . get ( ) ) ?;
31+ let start_col = u32:: try_from ( span. start . col . get ( ) ) ?;
32+ let end_line = u32:: try_from ( span. end . line . get ( ) ) ?;
33+ let end_col = u32:: try_from ( span. end . col . get ( ) ) ?;
3834
3935 Ok ( (
4036 lsp_types:: Position {
@@ -48,43 +44,14 @@ pub fn span_to_positions(
4844 ) )
4945}
5046
51- #[ allow( dead_code) ]
52- /// Convert pair of [`tower_lsp_server::lsp_types::Position`] to [`simplicityhl::error::Span`]
53- pub fn positions_to_span (
54- positions : ( lsp_types:: Position , lsp_types:: Position ) ,
55- ) -> Result < simplicityhl:: error:: Span > {
56- let start_line = NonZeroUsize :: new ( ( positions. 0 . line + 1 ) as usize )
57- . ok_or_else ( || Error :: invalid_params ( "start line must be non-zero" . to_string ( ) ) ) ?;
58-
59- let start_col = NonZeroUsize :: new ( ( positions. 0 . character + 1 ) as usize )
60- . ok_or_else ( || Error :: invalid_params ( "start column must be non-zero" . to_string ( ) ) ) ?;
61-
62- let end_line = NonZeroUsize :: new ( ( positions. 1 . line + 1 ) as usize )
63- . ok_or_else ( || Error :: invalid_params ( "end line must be non-zero" . to_string ( ) ) ) ?;
64-
65- let end_col = NonZeroUsize :: new ( ( positions. 1 . character + 1 ) as usize )
66- . ok_or_else ( || Error :: invalid_params ( "end column must be non-zero" . to_string ( ) ) ) ?;
67- Ok ( simplicityhl:: error:: Span {
68- start : simplicityhl:: error:: Position {
69- line : start_line,
70- col : start_col,
71- } ,
72- end : simplicityhl:: error:: Position {
73- line : end_line,
74- col : end_col,
75- } ,
76- } )
77- }
78-
7947/// Convert [`tower_lsp_server::lsp_types::Position`] to [`simplicityhl::error::Span`]
8048///
8149/// Useful when [`tower_lsp_server::lsp_types::Position`] represents some singular point.
82- pub fn position_to_span ( position : lsp_types:: Position ) -> Result < simplicityhl:: error:: Span > {
83- let start_line = NonZeroUsize :: new ( ( position. line + 1 ) as usize )
84- . ok_or_else ( || Error :: invalid_params ( "start line must be non-zero" . to_string ( ) ) ) ?;
85-
86- let start_col = NonZeroUsize :: new ( ( position. character + 1 ) as usize )
87- . ok_or_else ( || Error :: invalid_params ( "start column must be non-zero" . to_string ( ) ) ) ?;
50+ pub fn position_to_span (
51+ position : lsp_types:: Position ,
52+ ) -> Result < simplicityhl:: error:: Span , LspError > {
53+ let start_line = NonZeroUsize :: try_from ( ( position. line + 1 ) as usize ) ?;
54+ let start_col = NonZeroUsize :: try_from ( ( position. character + 1 ) as usize ) ?;
8855
8956 Ok ( simplicityhl:: error:: Span {
9057 start : simplicityhl:: error:: Position {
@@ -161,11 +128,13 @@ pub fn get_comments_from_lines(line: u32, rope: &Rope) -> String {
161128pub fn find_related_call (
162129 functions : & [ & parse:: Function ] ,
163130 token_span : simplicityhl:: error:: Span ,
164- ) -> std :: result :: Result < simplicityhl:: parse:: Call , & ' static str > {
131+ ) -> Result < Option < simplicityhl:: parse:: Call > , LspError > {
165132 let func = functions
166133 . iter ( )
167134 . find ( |func| span_contains ( func. span ( ) , & token_span) )
168- . ok_or ( "given span not inside function" ) ?;
135+ . ok_or ( LspError :: CallNotFound (
136+ "Span of the call is not inside function." . into ( ) ,
137+ ) ) ?;
169138
170139 let call = parse:: ExprTree :: Expression ( func. body ( ) )
171140 . pre_order_iter ( )
@@ -178,16 +147,15 @@ pub fn find_related_call(
178147 }
179148 } )
180149 . filter ( |( _, span) | span_contains ( span, & token_span) )
181- . map ( |( call, _) | call)
182- . last ( )
183- . ok_or ( "no related call found" ) ?;
150+ . map ( |( call, _) | call. clone ( ) )
151+ . last ( ) ;
184152
185- Ok ( call. to_owned ( ) )
153+ Ok ( call)
186154}
187155
188156pub fn get_call_span (
189157 call : & simplicityhl:: parse:: Call ,
190- ) -> std :: result :: Result < simplicityhl:: error:: Span , std :: num :: TryFromIntError > {
158+ ) -> Result < simplicityhl:: error:: Span , LspError > {
191159 let length = call. name ( ) . to_string ( ) . len ( ) ;
192160
193161 let end_column = usize:: from ( call. span ( ) . start . col ) + length;
0 commit comments