@@ -203,20 +203,60 @@ def getOutputPlace(syntax=None, name="SQLTools Result"):
203203 return resultContainer
204204
205205
206- def getSelection ():
206+ def getSelectionText ():
207207 text = []
208- if View (). sel ():
209- for region in View (). sel ():
210- if region . empty ():
211- if not settings . get ( 'expand_to_paragraph' , False ) :
212- text . append ( View (). substr ( View (). line ( region )))
213- else :
214- text . append ( View (). substr ( expand_to_paragraph ( View (), region . b )))
215- else :
216- text . append ( View (). substr ( region ))
208+
209+ selectionRegions = getSelectionRegions ()
210+
211+ if not selectionRegions :
212+ return text
213+
214+ for region in selectionRegions :
215+ text . append ( View (). substr ( region ))
216+
217217 return text
218218
219219
220+ def getSelectionRegions ():
221+ selectedRegions = []
222+
223+ if not View ().sel ():
224+ return None
225+
226+ # If we would need to expand the empty selection, then which type:
227+ # 'file', 'view' = use text of current view
228+ # 'paragraph' = paragraph(s) (text between newlines)
229+ # 'line' = current line(s)
230+ expandTo = settings .get ('expand_to' , 'file' )
231+ if not expandTo :
232+ expandTo = 'file'
233+
234+ # keep compatibility with previous settings
235+ expandToParagraph = settings .get ('expand_to_paragraph' )
236+ if expandToParagraph is True :
237+ expandTo = 'paragraph'
238+
239+ expandTo = str (expandTo ).strip ()
240+ if expandTo not in ['file' , 'view' , 'paragraph' , 'line' ]:
241+ expandTo = 'file'
242+
243+ for region in View ().sel ():
244+ if region .empty ():
245+ if expandTo in ['file' , 'view' ]:
246+ # no point in further iterating over selections, just use entire file
247+ return [sublime .Region (0 , View ().size ())]
248+ elif expandTo == 'paragraph' :
249+ selectedRegions .append (expand_to_paragraph (View (), region .b ))
250+ else :
251+ # expand to line
252+ selectedRegions .append (View ().line (region ))
253+ else :
254+ # use the user selected text
255+ selectedRegions .append (region )
256+
257+ return selectedRegions
258+
259+
220260def getCurrentSyntax ():
221261 view = View ()
222262 currentSyntax = None
@@ -485,7 +525,7 @@ def run():
485525 return
486526
487527 Window ().status_message (MESSAGE_RUNNING_CMD )
488- ST .conn .explainPlan (getSelection (), createOutput ())
528+ ST .conn .explainPlan (getSelectionText (), createOutput ())
489529
490530
491531class StExecute (WindowCommand ):
@@ -496,7 +536,7 @@ def run():
496536 return
497537
498538 Window ().status_message (MESSAGE_RUNNING_CMD )
499- ST .conn .execute (getSelection (), createOutput ())
539+ ST .conn .execute (getSelectionText (), createOutput ())
500540
501541
502542class StExecuteAll (WindowCommand ):
@@ -514,10 +554,12 @@ def run():
514554class StFormat (TextCommand ):
515555 @staticmethod
516556 def run (edit ):
517- for region in View ().sel ():
518- # if selection region is empty, use whole file as region
519- if region .empty ():
520- region = sublime .Region (0 , View ().size ())
557+ selectionRegions = getSelectionRegions ()
558+
559+ if not selectionRegions :
560+ return
561+
562+ for region in selectionRegions :
521563 textToFormat = View ().substr (region )
522564 View ().replace (edit , region , Utils .formatSql (textToFormat , settings .get ('format' , {})))
523565
@@ -550,7 +592,7 @@ def cb(index):
550592class StSaveQuery (WindowCommand ):
551593 @staticmethod
552594 def run ():
553- query = getSelection ()
595+ query = getSelectionText ()
554596
555597 def cb (alias ):
556598 queries .add (alias , query )
0 commit comments