Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.

Commit b56989c

Browse files
committed
Add more options for expanding the empty selection
Instead of config option `expand_to_paragraph`, introduce new option called `expand_to`, which can be configured to expand empty selection to: - `line` - current line(s) - `paragraph` - current paragraph(s) - `file` - current file (view)
1 parent 0d26c82 commit b56989c

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

SQLTools.py

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
220260
def 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

491531
class 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

502542
class StExecuteAll(WindowCommand):
@@ -514,10 +554,12 @@ def run():
514554
class 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):
550592
class 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

Comments
 (0)