@@ -17,22 +17,18 @@ function M.check_external_formatter()
1717 if not M .config .external_formatter .enabled then
1818 return false
1919 end
20-
2120 local cmd = M .config .external_formatter .command
2221 local handle = io.popen (" which " .. cmd .. " 2>/dev/null" )
2322 local result = handle :read (" *a" )
2423 handle :close ()
25-
2624 M .external_available = result ~= " "
27-
2825 if not M .external_available then
2926 vim .notify (
3027 " External SQL formatter '" .. cmd .. " ' not found. Install with: pip install sqlparse" ,
3128 vim .log .levels .WARN ,
3229 { title = " SQL Formatter" }
3330 )
3431 end
35-
3632 return M .external_available
3733end
3834
@@ -41,23 +37,18 @@ function M.format_with_external(text)
4137 if not M .external_available then
4238 return nil , " External formatter not available"
4339 end
44-
4540 local cmd = M .config .external_formatter .command
4641 local args = table.concat (M .config .external_formatter .args , " " )
4742 local full_cmd = string.format (" echo %s | %s %s" , vim .fn .shellescape (text ), cmd , args )
48-
4943 local handle = io.popen (full_cmd )
5044 if not handle then
5145 return nil , " Failed to execute formatter command"
5246 end
53-
5447 local result = handle :read (" *a" )
5548 local success = handle :close ()
56-
5749 if not success then
5850 return nil , " Formatter command failed"
5951 end
60-
6152 return result :gsub (" %s+$" , " " ), nil -- Remove trailing whitespace
6253end
6354
@@ -67,26 +58,20 @@ function M.format_with_lua(text)
6758 local formatted_lines = {}
6859 local indent_level = 0
6960 local indent = M .config .indent
70-
7161 for _ , line in ipairs (lines ) do
7262 local trimmed = line :match (" ^%s*(.-)%s*$" )
73-
7463 if trimmed ~= " " and not utils .is_comment_line (trimmed ) then
7564 -- Handle keywords that decrease indent
7665 if trimmed :upper ():match (" ^(END|ELSE|ELSIF|WHEN)%s" ) then
7766 indent_level = math.max (0 , indent_level - 1 )
7867 end
79-
8068 -- Apply current indentation
8169 local formatted_line = string.rep (indent , indent_level ) .. trimmed
82-
8370 -- Format keywords to uppercase if configured
8471 if M .config .uppercase then
8572 formatted_line = M .format_keywords (formatted_line )
8673 end
87-
8874 table.insert (formatted_lines , formatted_line )
89-
9075 -- Handle keywords that increase indent
9176 if trimmed :upper ():match (" ^(SELECT|FROM|WHERE|JOIN|INNER|LEFT|RIGHT|FULL|CASE|BEGIN|IF|LOOP|WHILE)%s" ) or
9277 trimmed :upper ():match (" (THEN|ELSE|DO)%s*$" ) then
@@ -96,7 +81,6 @@ function M.format_with_lua(text)
9681 table.insert (formatted_lines , trimmed )
9782 end
9883 end
99-
10084 return table.concat (formatted_lines , " \n " )
10185end
10286
@@ -112,12 +96,10 @@ function M.format_keywords(line)
11296 " decimal" , " float" , " double" , " boolean" , " date" , " datetime" , " timestamp" ,
11397 " case" , " when" , " then" , " else" , " end" , " if" , " begin" , " commit" , " rollback"
11498 }
115-
11699 for _ , keyword in ipairs (keywords ) do
117100 local pattern = " \\ b" .. keyword .. " \\ b"
118101 line = line :gsub (pattern , keyword :upper ())
119102 end
120-
121103 return line
122104end
123105
@@ -132,40 +114,31 @@ function M.format_sql(text)
132114 -- Fall back to Lua formatter
133115 end
134116 end
135-
136117 return M .format_with_lua (text )
137118end
138119
139120-- Format current buffer
140121function M .format_buffer ()
141122 local buf = vim .api .nvim_get_current_buf ()
142-
143123 if not utils .is_sql_buffer (buf ) then
144124 vim .notify (" Not a SQL buffer" , vim .log .levels .WARN )
145125 return
146126 end
147-
148127 local lines = vim .api .nvim_buf_get_lines (buf , 0 , - 1 , false )
149128 local text = table.concat (lines , " \n " )
150-
151129 if text :match (" ^%s*$" ) then
152130 vim .notify (" Buffer is empty" , vim .log .levels .INFO )
153131 return
154132 end
155-
156133 local formatted = M .format_sql (text )
157134 local new_lines = vim .split (formatted , " \n " )
158-
159135 -- Save cursor position
160136 local cursor = vim .api .nvim_win_get_cursor (0 )
161-
162137 vim .api .nvim_buf_set_lines (buf , 0 , - 1 , false , new_lines )
163-
164138 -- Restore cursor position (with bounds checking)
165139 local line_count = vim .api .nvim_buf_line_count (buf )
166140 cursor [1 ] = math.min (cursor [1 ], line_count )
167141 vim .api .nvim_win_set_cursor (0 , cursor )
168-
169142 if M .config .notify .enabled then
170143 vim .notify (" SQL formatted successfully" , vim .log .levels .INFO , { title = " SQL Formatter" })
171144 end
@@ -176,12 +149,9 @@ function M.format_range(start_line, end_line)
176149 local buf = vim .api .nvim_get_current_buf ()
177150 local lines = vim .api .nvim_buf_get_lines (buf , start_line - 1 , end_line , false )
178151 local text = table.concat (lines , " \n " )
179-
180152 local formatted = M .format_sql (text )
181153 local new_lines = vim .split (formatted , " \n " )
182-
183154 vim .api .nvim_buf_set_lines (buf , start_line - 1 , end_line , false , new_lines )
184-
185155 if M .config .notify .enabled then
186156 vim .notify (" SQL range formatted successfully" , vim .log .levels .INFO , { title = " SQL Formatter" })
187157 end
0 commit comments