Skip to content

Commit

Permalink
Add the following features:
Browse files Browse the repository at this point in the history
- If there is a comment at the end of the line, place the semicolon before the comment
- Don't place a semicolon if one is already present
  • Loading branch information
Maurice Zhang committed Jun 26, 2015
1 parent 5688b82 commit 06983ab
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions AppendSemiColon.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import sublime_plugin;
import sublime_plugin

class AppendSemiColonCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
line_end = self.view.line(region).end()
if self.view.substr(line_end - 1) != ';':
self.view.insert(edit, line_end, ';')
def run(self, edit):

def insert_semicolon(point):
self.view.insert(edit, point, ';')

def is_semicolon(point):
return self.view.substr(point) == ';'

for region in self.view.sel():
line = self.view.line(region)
line_begin = line.begin()
line_end = line.end()

while(self.view.substr(line_end - 1).isspace() and line_end != line_begin): # go to the first character from the end that isn't whitespace
line_end -= 1

if line_end == line_begin:
continue

if self.view.match_selector(line_end - 1, 'comment'):
point = self.view.extract_scope(line_end - 1).a - 1

if point < line_begin:
continue

while(self.view.substr(point).isspace() and point != line_begin): # go to the first character before the comment that isn't whitespace
point -= 1

if not self.view.substr(point).isspace() and not is_semicolon(point):
insert_semicolon(point + 1)

elif not is_semicolon(line_end - 1):
insert_semicolon(line_end)

0 comments on commit 06983ab

Please sign in to comment.