Skip to content

Commit

Permalink
Merge pull request #849 from divmain/pedantic_commit
Browse files Browse the repository at this point in the history
Pedantic commit
  • Loading branch information
asfaltboy committed Feb 11, 2018
2 parents 3a0f32b + c0085fa commit 06b1e7f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
17 changes: 17 additions & 0 deletions GitSavvy.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@

"use_syntax_for_commit_editmsg": false,

/*
https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#Commit-Guidelines
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_ruler": 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
93 changes: 93 additions & 0 deletions core/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sublime
from sublime_plugin import WindowCommand, TextCommand
from sublime_plugin import EventListener

from ..git_command import GitCommand
from ...common import util
Expand Down Expand Up @@ -139,6 +140,98 @@ def run(self, edit):
})


class GsPedanticEnforceEventListener(EventListener):
"""
Set regions to worn for Pedantic commits
"""

def on_selection_modified(self, view):
if 'make_commit' not in view.settings().get('syntax'):
return

savvy_settings = sublime.load_settings("GitSavvy.sublime-settings")
if not savvy_settings.get('pedantic_commit'):
return

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.warning_length = 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]

if savvy_settings.get('pedantic_commit_ruler'):
self.view.settings().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

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

new_rulers = []
if on_first_line:
new_rulers.append(self.first_line_limit)

if on_message_body:
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 length > self.first_line_limit:
warning_lines.append(sublime.Region(
first_line.a + self.first_line_limit,
min(first_line.a + self.first_line_limit + self.warning_length, first_line.b)))

if length > self.first_line_limit + self.warning_length:
illegal_lines.append(sublime.Region(first_line.a + self.first_line_limit + self.warning_length, 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].begin())
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 length > self.body_line_limit:
warning_lines.append(sublime.Region(
line.a + self.body_line_limit,
min(line.a + self.body_line_limit + self.warning_length, line.b)))

if self.body_line_limit + self.warning_length < length:
illegal_lines.append(sublime.Region(line.a + self.body_line_limit + self.warning_length, line.b))

return [warning_lines, illegal_lines]


class GsCommitViewDoCommitCommand(TextCommand, GitCommand):

"""
Expand Down
3 changes: 1 addition & 2 deletions syntax/make_commit.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
// syntax specific
"gutter": true,
"line_numbers": true,
"word_wrap": true,
"wrap_width": 100
"word_wrap": true
}

0 comments on commit 06b1e7f

Please sign in to comment.