Skip to content

Instantly share code, notes, and snippets.

@adison
Last active August 4, 2017 17:34
Show Gist options
  • Save adison/d6206c865c1dd3be9481 to your computer and use it in GitHub Desktop.
Save adison/d6206c865c1dd3be9481 to your computer and use it in GitHub Desktop.
remove comments in sublime text
import sublime_plugin
import sublime
# code from http://stackoverflow.com/questions/26286879/can-we-make-use-of-syntax-highlighting-feature-to-remove-all-comments-from-a-sou
# how to use
# save as remove_commands in sublime text package/user folder,
# open foloder from [Preferences] > [Browser Packages], and you will find [User] folder
# save this file in [User] foler.
#
# after file saved, open command panel in sublime text, aka ctrl + `
# you can input view.run_command('remove_comments') to remove comments in current tab/file
# meanwhile, view.run_command('purify_sql') will "purify" the code and retunr SQL instrucntions within.
class RemoveCommentsCommand(sublime_plugin.TextCommand):
def run(self, edit):
comments = self.view.find_by_selector('comment')
for region in reversed(comments):
# use \n replace comments avoid of lines join
self.view.replace(edit, region, '\n')
# command to get pure sql command for any usage
class PurifySqlCommand(sublime_plugin.TextCommand):
def run(self, edit):
# this is magic, i have no idea where the file is
self.view.set_syntax_file('Packages/SQL/SQL.tmLanguage')
comments = self.view.find_by_selector('comment')
line_endings = '\n'
if self.view.settings().get('default_line_ending') == 'window':
line_endings = '\r\n'
for region in reversed(comments):
# use \n replace comments avoid of lines join
self.view.replace(edit, region, line_endings)
# delete any before first "
lines = self.view.find_all('^[^"]*.', sublime.IGNORECASE)
for line in reversed(lines):
self.view.erase(edit, line)
# delete any after last one "
lines = self.view.find_all('"*?[^"]*$', sublime.IGNORECASE)
for line in reversed(lines):
self.view.erase(edit, line)
@adison
Copy link
Author

adison commented Aug 11, 2015

sublime text plugin for remove comments
add an advanced command to purify sql instruction for sql test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment