Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions code_getter/getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def initialize(cls, view):
return PythonCodeGetter(view)
elif syntax == "julia":
return JuliaCodeGetter(view)
elif syntax == "matlab":
return MatlabCodeGetter(view)
else:
return CodeGetter(view)

Expand Down Expand Up @@ -198,6 +200,22 @@ def backward_expand(self, s, pattern=r"[+\-*/](?=\s*$)", scope="keyword.operator
break

return s

def backward_sq_bracket_expand(self, s, pattern=r"[\[](?=\s*$)"):
# backward_expand previous lines ending with operators

view = self.view
row = view.rowcol(s.begin())[0]
while row > 0:
row = row - 1
line = view.line(view.text_point(row, 0))
if re.search(pattern, view.substr(line)):
endpt = self.find_inline(r"\S(?=\s*$)", line.begin()).begin()
if endpt != -1:
s = sublime.Region(line.begin(), s.end())
continue
break
return s

def block_expand(self, s):
# expand block based on block_start_pattern and block_end_pattern
Expand Down Expand Up @@ -375,6 +393,75 @@ def expand_line(self, s):
s = sublime.Region(s.begin(), line.end())
break

elif re.match(r"\s*#=", thiscmd) and not re.match(r".*=#\s*$", thiscmd):
indentation = re.match(r"^(\s*)", thiscmd).group(1)
endline = view.find(r"^" + indentation + r"=#", s.begin())
s = sublime.Region(s.begin(), view.line(endline.end()).end())

elif re.match(r"\s*#>>", thiscmd) and not re.match(r".*#<<\s*$", thiscmd):
indentation = re.match(r"^(\s*)", thiscmd).group(1)
endline = view.find(r"^" + indentation + r"#<<", s.begin())
s = sublime.Region(s.begin(), view.line(endline.end()).end())

elif re.match(r".*].*$", thiscmd) and not re.match(r".*\[.*$", thiscmd):
s = self.backward_sq_bracket_expand(s, pattern=r"[\[](?=\s*)")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not very accurate. Imagine we have a string s = "]".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sry didn't realise it would be updating the pull as I make changes to my repository. The square bracket is a work in progress from today.

What is the correct way to do these pull requests? Creating a separate branch for each pull request?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. It is usually a better idea to create a new branch for pull requests.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But at least in matlab there are no other uses for square brackets than array definitions so solving the issue you raised should be enough.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I argue that we shouldn’t allow this though. The code is not readable with that style and you should first run a code formatter.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's actually how I format my matrices most of the time.

I personally want the result to be a live matlab interpreter where I can send commands as I write the file. But I will be happy to keep it in my fork with more brute force algorithms, leaving your elegant code as is.

image


else:
s = self.forward_expand(s, pattern=r"[+\-*/](?=\s*$)")

return s


class MatlabCodeGetter(CodeGetter):

def expand_line(self, s):
view = self.view
if view.score_selector(s.begin(), "string"):
return s

s_block = self.block_expand(s)
if s_block != s:
return s_block

thiscmd = view.substr(s)
row = view.rowcol(s.begin())[0]
lastrow = view.rowcol(view.size())[0]

keywords = [
"for", "while", "switch", "try", "if", "parfor",
"function"
]
if (re.match(r"\s*\b(?:{})\b".format("|".join(keywords)), thiscmd) and
not re.match(r".*\bend\b\s*$", thiscmd)):
indentation = re.match(r"^(\s*)", thiscmd).group(1)
endline = view.find(r"^" + indentation + r"\bend\b", s.begin())
s = sublime.Region(s.begin(), view.line(endline.end()).end())

elif re.match(r"\s*\bimport\b", thiscmd):
row = view.rowcol(s.begin())[0]
lastrow = view.rowcol(view.size())[0]
while row <= lastrow:
line = view.line(view.text_point(row, 0))
if re.match(r".*[:,]\s*$", view.substr(line)):
row = row + 1
else:
s = sublime.Region(s.begin(), line.end())
break

elif re.match(r"\s*%{\s*$", thiscmd):
indentation = re.match(r"^(\s*)", thiscmd).group(1)
endline = view.find(r"^" + indentation + r"%}", s.begin())
no_cmts = True,
s = sublime.Region(s.begin(), view.line(endline.end()).end())

elif re.match(r"\s*%>>", thiscmd) and not re.match(r".*%<<\s*$", thiscmd):
indentation = re.match(r"^(\s*)", thiscmd).group(1)
endline = view.find(r"^" + indentation + r"%<<", s.begin())
s = sublime.Region(s.begin(), view.line(endline.end()).end())

elif re.match(r".*].*$", thiscmd) and not re.match(r".*\[.*$", thiscmd):
s = self.backward_sq_bracket_expand(s, pattern=r"[\[](?=\s*)")

else:
s = self.forward_expand(s, pattern=r"[+\-*/](?=\s*$)")

Expand Down
6 changes: 6 additions & 0 deletions code_sender/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def initialize(cls, view, **kwargs):
return PythonCodeSender(view, **kwargs)
elif syntax == "julia":
return JuliaCodeSender(view, **kwargs)
elif syntax == "matlab":
return MatlabCodeSender(view, **kwargs)
else:
return CodeSender(view, **kwargs)

Expand Down Expand Up @@ -288,3 +290,7 @@ def send_to_terminus(self, cmd):
class JuliaCodeSender(CodeSender):

pass

class MatlabCodeSender(CodeSender):

pass
3 changes: 2 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Settings:
"text.html.markdown.rmarkdown": "rmd",
"text.html.markdown": "md",
"source.python": "python",
"source.julia": "julia"
"source.julia": "julia",
"source.matlab": "matlab"
}

def __init__(self, view):
Expand Down