Skip to content

Commit

Permalink
Feature: Pedantic commit messages
Browse files Browse the repository at this point in the history
fix #41
  • Loading branch information
stoivo committed Feb 4, 2018
1 parent 8924d13 commit 3b88c1e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
15 changes: 15 additions & 0 deletions GitSavvy.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@

"use_syntax_for_commit_editmsg": false,

/*
Add a distinct style guide for the commit messages:
First line should be max 50 characters
Second line should be empty
Any subsequent lines should be max 80 characters
It will use 'invalid.deprecated.line-too-long.git-commit' scope by default.
The warning is will be outlined instead of fully marked
*/
"pedantic_commit": true,
"pedantic_commit_first_line_length": 50,
"pedantic_commit_message_line_length": 80,
"pedantic_commit_warning_length": 20,

/*
Change this to `false` to suppress the input in the panel output.
*/
Expand Down
74 changes: 59 additions & 15 deletions core/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,40 +153,84 @@ def on_selection_modified(self, view):
if not savvy_settings.get('pedantic_commit'):
return

first_line_limit = 50
body_line_limit = 80
warrning_size = 20
self.view = view
self.first_line_limit = savvy_settings.get('pedantic_commit_first_line_length')
self.body_line_limit = savvy_settings.get('pedantic_commit_message_line_length')
self.warrning_size = savvy_settings.get('pedantic_commit_warning_length')

self.comment_start_region = self.view.find_all('^#')
self.first_comment_line = None
if self.comment_start_region:
self.first_comment_line = self.view.rowcol(self.comment_start_region[0].begin())[0]


view_setting = self.view.settings()
view_setting.set("rulers", self.find_rulers())

waring, illegal = self.find_too_long_lines()
self.view.add_regions('make_commit_warning', waring, scope='invalid.deprecated.line-too-long.git-commit', flags=sublime.DRAW_NO_FILL)
self.view.add_regions('make_commit_illegal', illegal, scope='invalid.deprecated.line-too-long.git-commit')

def find_rulers(self):
on_first_line = False
on_message_body = False
comment_start_region = view.find_all('^#')

for region in view.sel():
first_line = view.rowcol(region.begin())[0]
last_line = view.rowcol(region.end())[0]
for region in self.view.sel():
first_line = self.view.rowcol(region.begin())[0]
last_line = self.view.rowcol(region.end())[0]

if on_first_line or first_line == 0:
on_first_line = True

if comment_start_region:
first_comment_line = view.rowcol(comment_start_region[0].begin())[0]
if first_line in range(2, first_comment_line) or last_line in range(2, first_comment_line):
if self.first_comment_line:
if first_line in range(2, self.first_comment_line) or last_line in range(2, self.first_comment_line):
on_message_body = True
else:
if first_line >= 2 or last_line >= 2:
on_message_body = True

view_setting = view.settings()
rulers = view_setting.get("rulers")
new_rulers = []
if on_first_line:
new_rulers.append(first_line_limit)
new_rulers.append(self.first_line_limit)

if on_message_body:
new_rulers.append(body_line_limit)
new_rulers.append(self.body_line_limit)

return new_rulers

def find_too_long_lines(self):
warning_lines = []
illegal_lines = []

first_line = self.view.lines(sublime.Region(0,0))[0]
length = first_line.b - first_line.a
if self.first_line_limit < length:
warning_lines.append(sublime.Region(
first_line.a + self.first_line_limit,
min(first_line.a+self.first_line_limit+self.warrning_size, first_line.b)))

if self.first_line_limit+self.warrning_size < length:
illegal_lines.append(sublime.Region(first_line.a + self.first_line_limit+self.warrning_size, first_line.b))

# Add second line to illegal
illegal_lines.append(sublime.Region(self.view.text_point(1, 0), self.view.text_point(2, 0) - 1))

if self.first_comment_line:
body_region = sublime.Region(self.view.text_point(2, 0), self.comment_start_region[0])
else:
body_region = sublime.Region(self.view.text_point(2, 0), self.view.size())

for line in self.view.lines(body_region):
length = line.b - line.a
if self.body_line_limit < length:
warning_lines.append(sublime.Region(
line.a + self.body_line_limit,
min(line.a+self.body_line_limit+self.warrning_size, line.b)))

view_setting.set("rulers", new_rulers)
if self.body_line_limit+self.warrning_size < length:
illegal_lines.append(sublime.Region(line.a + self.body_line_limit+self.warrning_size, line.b))

return [warning_lines, illegal_lines]

class GsCommitViewDoCommitCommand(TextCommand, GitCommand):

Expand Down

0 comments on commit 3b88c1e

Please sign in to comment.