Skip to content

Commit

Permalink
Initial work on rewriting the indention plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
thatsIch committed Jan 4, 2017
1 parent 6e0e0c1 commit 50b5f1e
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 5 deletions.
69 changes: 64 additions & 5 deletions indentrainmeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,65 @@
import sublime_plugin


FOLD_MARKER_EXP = re.compile("^([ \\t]*)(;;.*)$")
SECTION_EXP = re.compile("^([ \\t]*)(\\[.*)$")
EMPTY_LINE_EXP = re.compile("^\\s*$")
COMMENT_EXP = re.compile("^\\s*(;.*)")


class IndentType: # pylint: disable=R0903; enum
"""."""

# lvl 0, lvl 1, lvl 1 or 2
Initial, FoldMarker, Section = range(1, 4)


def calc_line_indention_depth(line, context, context_depth):
"""."""
empty_line_match = EMPTY_LINE_EXP.match(line)
if empty_line_match:
return (0, context, context_depth)

folder_marker_match = FOLD_MARKER_EXP.match(line)
if folder_marker_match:
return (0, IndentType.FoldMarker, 1)

comment_match = COMMENT_EXP.match(line)
if comment_match:
return (context_depth, context, context_depth)

section_match = SECTION_EXP.match(line)
if section_match:
if context == IndentType.Section:
return (context_depth - 1, IndentType.Section, context_depth)
else:
return (context_depth, IndentType.Section, context_depth + 1)

# key value case
return (context_depth, context, context_depth)


def replace_line(view, edit, line, context_depth):
"""."""
stripped = line.lstrip()
replacement = "\t" * context_depth + stripped
view.replace(edit, line, replacement)


def get_current_indention(line):
"""."""
settings = sublime.load_settings("Rainmeter.sublime-settings")
tab_size = settings.get("tab_size", 2)
expanded = line.expandtabs(tab_size)
stripped = line.lstrip()
start_index = expanded.find(stripped)
whitespaces = expanded.count(" ", 0, start_index)
tabs = whitespaces / tab_size

return tabs



class RainmeterIndentCommand(sublime_plugin.TextCommand):
"""
Indent a Rainmeter file so code folding is possible in a sensible way.
Expand All @@ -29,6 +88,7 @@ class RainmeterIndentCommand(sublime_plugin.TextCommand):

def __get_selected_region(self):
# If nothing is selected, apply to whole buffer
# TODO should check every caret
if self.view.sel()[0].a == self.view.sel()[-1].b:
regions = [sublime.Region(0, self.view.size())]
# If something is selected, apply only to selected regions
Expand All @@ -37,11 +97,11 @@ def __get_selected_region(self):

return regions


def run(self, edit): #pylint: disable=R0201; sublime text API, no need for class reference
def run(self, edit): # pylint: disable=R0201; sublime text API, no need for class reference
"""Called when the command is run."""
regions = self.__get_selected_region()

lines = self.view.lines(sublime.Region(0, self.view.size()))
for region in regions:
# Get numbers of regions' lines
reg_lines = self.view.lines(region)
Expand All @@ -51,7 +111,6 @@ def run(self, edit): #pylint: disable=R0201; sublime text API, no need for class
current_indent = -1
adjustment = -1

lines = self.view.lines(sublime.Region(0, self.view.size()))
for i in line_nums:
line = lines[i]
line_content = self.view.substr(line)
Expand Down Expand Up @@ -84,7 +143,7 @@ def run(self, edit): #pylint: disable=R0201; sublime text API, no need for class
"\t" * (current_indent + 2 + adjustment) +
stripped_line)

def is_enabled(self): #pylint: disable=R0201; sublime text API, no need for class reference
def is_enabled(self): # pylint: disable=R0201; sublime text API, no need for class reference
"""
Return True if the command is able to be run at this time.
Expand All @@ -96,7 +155,7 @@ def is_enabled(self): #pylint: disable=R0201; sublime text API, no need for clas

return israinmeter > 0

def description(self): #pylint: disable=R0201; sublime text API, no need for class reference
def description(self): # pylint: disable=R0201; sublime text API, no need for class reference
"""
Return a description of the command with the given arguments.
Expand Down
107 changes: 107 additions & 0 deletions tests/test_indent_rainmeter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""This module is for testing the indent rainmeter module."""


import sys

from unittest import TestCase

INDENT = sys.modules["Rainmeter.indentrainmeter"]


def indention_depth_from_initial(line):
return INDENT.calc_line_indention_depth(line, INDENT.IndentType.Initial, 0)


def indention_depth_from_fold_marker(line):
return INDENT.calc_line_indention_depth(line, INDENT.IndentType.FoldMarker, 1)


def indention_depth_from_section(line):
return INDENT.calc_line_indention_depth(line, INDENT.IndentType.Section, 1)


class TestCalcLineIndentionDepthFromInitial(TestCase):
"""."""

def test_with_empty_line(self):
line = ""
indention_depth = indention_depth_from_initial(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.Initial, 0))

def test_with_comment(self):
line = "; This is a comment"
indention_depth = indention_depth_from_initial(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.Initial, 0))

def test_with_fold_marker(self):
line = ";; This is a fold marker"
indention_depth = indention_depth_from_initial(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.FoldMarker, 1))

def test_with_section(self):
line = "[Section]"
indention_depth = indention_depth_from_initial(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.Section, 1))

def test_with_key_value(self):
line = "Key = Value"
indention_depth = indention_depth_from_initial(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.Initial, 0))


class TestCalcLineIndentionDepthFromFoldMarker(TestCase):
"""."""

def test_with_empty_line(self):
line = ""
indention_depth = indention_depth_from_fold_marker(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.FoldMarker, 1))

def test_with_comment(self):
line = "; This is a comment"
indention_depth = indention_depth_from_fold_marker(line)
self.assertEqual(indention_depth, (1, INDENT.IndentType.FoldMarker, 1))

def test_with_fold_marker(self):
line = ";; This is a fold marker"
indention_depth = indention_depth_from_fold_marker(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.FoldMarker, 1))

def test_with_section(self):
line = "[Section]"
indention_depth = indention_depth_from_fold_marker(line)
self.assertEqual(indention_depth, (1, INDENT.IndentType.Section, 2))

def test_with_key_value(self):
line = "Key = Value"
indention_depth = indention_depth_from_fold_marker(line)
self.assertEqual(indention_depth, (1, INDENT.IndentType.FoldMarker, 1))


class TestCalcLineIndentionDepthFromSection(TestCase):
"""."""

def test_with_empty_line(self):
line = ""
indention_depth = indention_depth_from_section(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.Section, 1))

def test_with_comment(self):
line = "; This is a comment"
indention_depth = indention_depth_from_section(line)
self.assertEqual(indention_depth, (1, INDENT.IndentType.Section, 1))

def test_with_fold_marker(self):
line = ";; This is a fold marker"
indention_depth = indention_depth_from_section(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.FoldMarker, 1))

def test_with_section(self):
line = "[Section]"
indention_depth = indention_depth_from_section(line)
self.assertEqual(indention_depth, (0, INDENT.IndentType.Section, 1))

def test_with_key_value(self):
line = "Key = Value"
indention_depth = indention_depth_from_section(line)
self.assertEqual(indention_depth, (1, INDENT.IndentType.Section, 1))

0 comments on commit 50b5f1e

Please sign in to comment.