1+ --- @class PhpUseSort
2+ local M = {}
3+
14local ts = vim .treesitter
25local parsers = require (" nvim-treesitter.parsers" )
6+ local vim_diagnostic = vim .diagnostic
37
48local p = function (value )
59 print (vim .inspect (value ))
@@ -9,9 +13,6 @@ local t = function(node)
913 p (ts .get_node_text (node , 0 ))
1014end
1115
12- --- @class PhpUseSort
13- local M = {}
14-
1516local function sort_use_statements (use_statements , sort_order )
1617 table.sort (use_statements , function (a , b )
1718 local len_a , len_b = # a .statement , # b .statement
@@ -32,6 +33,20 @@ local function sort_use_statements(use_statements, sort_order)
3233 end )
3334end
3435
36+ local function remove_declared_but_not_used (row )
37+ local diag_text = " is declared but not used."
38+ local diagnostics = vim_diagnostic .get (0 , {
39+ lnum = row ,
40+ severity = vim_diagnostic .severity .HINT ,
41+ })
42+
43+ if not vim .tbl_isempty (diagnostics ) and string.find (diagnostics [1 ].message , diag_text ) then
44+ return true
45+ end
46+
47+ return false
48+ end
49+
3550local function parse_tree (parser )
3651 return parser :parse ()[1 ]
3752end
@@ -47,20 +62,13 @@ local function extract_use_statements(root, lang, rm_unused)
4762 for _ , matches , metadata in query :iter_matches (root , 0 ) do
4863 for _ , node in pairs (matches ) do
4964 local start_row , _ , end_row , _ = node :range ()
50- local statement = ts .get_node_text (node , 0 )
51- local diagnostics = vim .diagnostic .get (0 , { lnum = start_row , severity = vim .diagnostic .severity .HINT })
5265
53- if not vim .tbl_isempty (diagnostics ) then
54- if string.find (diagnostics [1 ].message , diag_text ) then
55- goto continue
56- end
66+ if not rm_unused or not remove_declared_but_not_used (start_row ) then
67+ local statement = ts .get_node_text (node , 0 )
68+ range .min = math.min (range .min , start_row + 1 )
69+ range .max = math.max (range .max , end_row + 1 )
70+ table.insert (use_statements , { statement = statement , node = node })
5771 end
58-
59- table.insert (use_statements , { statement = statement , node = node })
60- range .min = math.min (range .min , start_row + 1 )
61- range .max = math.max (range .max , end_row + 1 )
62-
63- :: continue::
6472 end
6573 end
6674
@@ -80,8 +88,8 @@ local function update_buffer(range, use_statements)
8088end
8189
8290local function setup_autocmd ()
83- local Config = require ( " php-use-sort.config " )
84- if not Config . options .autocmd then
91+ local options = M . get_config_options ( )
92+ if not options .autocmd then
8593 return
8694 end
8795 local group = vim .api .nvim_create_augroup (" PhpUseSort" , { clear = true })
@@ -105,8 +113,12 @@ local function setup_command()
105113 })
106114end
107115
116+ function M .get_config_options ()
117+ return require (" php-use-sort.config" ).options
118+ end
119+
108120function M .main (sort_order )
109- local Config = require ( " php-use-sort.config " )
121+ local options = M . get_config_options ( )
110122 local parser = parsers .get_parser ()
111123 local tree = parse_tree (parser )
112124
@@ -123,9 +135,9 @@ function M.main(sort_order)
123135 return
124136 end
125137
126- sort_order = sort_order ~= " " and sort_order or Config . options .order
138+ sort_order = sort_order ~= " " and sort_order or options .order
127139
128- local use_statements , range = extract_use_statements (root , lang , Config . options .rm_unused )
140+ local use_statements , range = extract_use_statements (root , lang , options .rm_unused )
129141
130142 sort_use_statements (use_statements , sort_order )
131143
@@ -136,14 +148,6 @@ function M.setup(options)
136148 require (" php-use-sort.config" ).setup (options .opts )
137149 setup_command ()
138150 setup_autocmd ()
139- p (vim .diagnostic .get (0 , {
140- severity = {
141- vim .diagnostic .severity .ERROR ,
142- vim .diagnostic .severity .WARN ,
143- vim .diagnostic .severity .INFO ,
144- vim .diagnostic .severity .HINT ,
145- },
146- }))
147151end
148152
149153return M
0 commit comments