From 8924d133db68fa66a5581f41fa6b49f137d8fd71 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 4 Feb 2018 01:38:57 +0100 Subject: [PATCH 1/2] Add rules to commit view if pedantic_commit is set --- core/commands/commit.py | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/core/commands/commit.py b/core/commands/commit.py index c67b78d17..6849916a0 100644 --- a/core/commands/commit.py +++ b/core/commands/commit.py @@ -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 @@ -139,6 +140,54 @@ 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 + + first_line_limit = 50 + body_line_limit = 80 + warrning_size = 20 + + 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] + + 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): + 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) + + if on_message_body: + new_rulers.append(body_line_limit) + + view_setting.set("rulers", new_rulers) + + class GsCommitViewDoCommitCommand(TextCommand, GitCommand): """ From c0085fae3a23036d09d38f5e5b97d0978e4c657a Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 4 Feb 2018 03:12:22 +0100 Subject: [PATCH 2/2] Feature: Pedantic commit messages fix #41 fix #423 --- GitSavvy.sublime-settings | 17 +++++++ core/commands/commit.py | 74 +++++++++++++++++++++++------ syntax/make_commit.sublime-settings | 3 +- 3 files changed, 77 insertions(+), 17 deletions(-) diff --git a/GitSavvy.sublime-settings b/GitSavvy.sublime-settings index f8359b008..a8bac7ca3 100755 --- a/GitSavvy.sublime-settings +++ b/GitSavvy.sublime-settings @@ -109,6 +109,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. */ diff --git a/core/commands/commit.py b/core/commands/commit.py index 6849916a0..6880df16b 100644 --- a/core/commands/commit.py +++ b/core/commands/commit.py @@ -153,39 +153,83 @@ 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.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 - 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 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)) - view_setting.set("rulers", new_rulers) + return [warning_lines, illegal_lines] class GsCommitViewDoCommitCommand(TextCommand, GitCommand): diff --git a/syntax/make_commit.sublime-settings b/syntax/make_commit.sublime-settings index 8c84f6ed5..b7c5e0b5b 100644 --- a/syntax/make_commit.sublime-settings +++ b/syntax/make_commit.sublime-settings @@ -13,6 +13,5 @@ // syntax specific "gutter": true, "line_numbers": true, - "word_wrap": true, - "wrap_width": 100 + "word_wrap": true }