forked from daeyun/vim-matlab
-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
274 lines (223 loc) · 9.54 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import hashlib
import os
import datetime
import errno
import re
import time
import collections
import neovim
from matlab_cli_controller import MatlabCliController
from python_vim_utils import PythonVimUtils as vim_helper
import python_vim_utils
__created__ = 'Mar 01, 2015'
__license__ = 'MPL 2.0'
__author__ = 'Daeyun Shin'
__email__ = "[email protected]"
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
@neovim.plugin
class VimMatlab(object):
def __init__(self, vim):
self.vim = vim
python_vim_utils.vim = vim
self.gui_controller = None
self.cli_controller = None
self.buffer_state = collections.defaultdict(dict)
self.function_name_pattern = \
re.compile(r'((?:^|\n[ \t]*)(?!%)[ \t]*(?:function(?:[ \t]|\.\.\.'
r'[ \t]*\n)(?:[^\(\n]|\.\.\.[ \t]*\n)*?|classdef(?:[ \t]'
r'|\.\.\.[ \t]*\n)(?:[^\<\n]|\.\.\.[ \t]*\n)*?))('
r'[a-zA-Z]\w*)((?:[ \t]|\.\.\.[ \t]*\n)*(?:\(|\<|\n|$))')
@neovim.command('MatlabPrintCellLines', sync=True)
def run_print_cell_lines(self):
lines = vim_helper.get_current_matlab_cell_lines(
ignore_matlab_comments=True)
vim_helper.echo_text("\n".join(lines))
@neovim.command('MatlabCliRunSelection', sync=True)
def run_selection_in_matlab_cli(self):
if self.cli_controller is None:
self.activate_cli()
self.matlab_write_function_files()
lines = vim_helper.get_selection(ignore_matlab_comments=True)
self.cli_controller.run_code(lines)
@neovim.command('MatlabCliRunLine', sync=True)
def run_current_line(self):
if self.cli_controller is None:
self.activate_cli()
self.matlab_write_function_files()
line = [vim_helper.get_current_matlab_line()]
self.cli_controller.run_code(line)
@neovim.command('MatlabCliRunCell', sync=True)
def run_cell_in_matlab_cli(self):
if self.cli_controller is None:
self.activate_cli()
self.matlab_write_function_files()
lines = vim_helper.get_current_matlab_cell_lines(
ignore_matlab_comments=True)
self.cli_controller.run_code(lines)
@neovim.command('MatlabCliActivateControls', sync=True)
def activate_cli(self):
if self.cli_controller is not None:
return
self.cli_controller = MatlabCliController()
@neovim.command('MatlabCliViewVarUnderCursor', sync=True)
def view_var_under_cursor(self):
if self.cli_controller is None:
self.activate_cli()
var = vim_helper.get_variable_under_cursor()
if var:
self.cli_controller.run_code(['printVarInfo({});'.format(var)])
@neovim.command('MatlabCliViewSelectedVar', sync=True)
def view_selected_var(self):
if self.cli_controller is None:
self.activate_cli()
var = vim_helper.get_selection()
if var:
self.cli_controller.run_code(['printVarInfo({});'.format(var)])
@neovim.command('MatlabCliCancel', sync=True)
def matlab_cli_cancel(self):
if self.cli_controller is None:
self.activate_cli()
self.cli_controller.send_ctrl_c()
@neovim.command('MatlabCliOpenInMatlabEditor', sync=True)
def matlab_cli_open_in_matlab_editor(self):
if self.cli_controller is None:
self.activate_cli()
path = vim_helper.get_current_file_path()
self.cli_controller.open_in_matlab_editor(path)
@neovim.command('MatlabCliOpenWorkspace', sync=True)
def matlab_cli_open_workspace(self):
if self.cli_controller is None:
self.activate_cli()
self.cli_controller.open_workspace()
@neovim.command('MatlabCliHelp', sync=True)
def matlab_cli_help(self):
if self.cli_controller is None:
self.activate_cli()
var = vim_helper.get_variable_under_cursor()
self.cli_controller.help_command(var)
@neovim.command('MatlabCliOpenVar', sync=True)
def matlab_cli_open_var(self):
if self.cli_controller is None:
self.activate_cli()
var = vim_helper.get_variable_under_cursor()
self.cli_controller.openvar(var)
@neovim.command('MatlabWriteFunctionFiles', sync=True)
def matlab_write_function_files(self):
options = vim_helper.get_options()
if 'split' in options:
group_name = options['split'][0]
else:
return
dir_path = os.path.join(
os.path.dirname(vim_helper.get_current_file_path()), group_name)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
existing_filenames = [name for name in os.listdir(dir_path) if
name.endswith('.m')]
function_blocks = vim_helper.get_function_blocks()
new_filenames = [name + '.m' for name in function_blocks.keys()]
unused_filenames = list(set(existing_filenames) - set(new_filenames))
for name in unused_filenames:
try:
os.remove(os.path.join(dir_path, name))
except:
pass
added_filenames = list(set(new_filenames) - set(existing_filenames))
for name in added_filenames:
content = function_blocks[os.path.splitext(name)[0]].strip()
with open(os.path.join(dir_path, name), 'w') as f:
f.write(content)
common_filenames = set.intersection(
*[set(existing_filenames), set(new_filenames)])
for name in common_filenames:
content = function_blocks[os.path.splitext(name)[0]].strip()
code_hash = hashlib.md5(content).hexdigest()
with open(os.path.join(dir_path, name), 'r+') as f:
file_hash = hashlib.md5(f.read().strip()).hexdigest()
f.seek(0)
if code_hash != file_hash:
f.write(content)
f.truncate()
@neovim.command('MatlabOpenTempScript', sync=True, nargs='*')
def open_temp_matlab_script(self, args):
dirname = os.path.join(os.path.expanduser('~'), '.vim-matlab/scratch/')
if not os.path.exists(dirname):
try:
os.makedirs(dirname)
except OSError as ex:
# In case of a race condition
if ex.errno != errno.EEXIST:
raise
timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
if len(args) > 0:
filename = "{}_{}.m".format(timestamp, args[0])
else:
filename = "{}.m".format(timestamp)
self.vim.command('edit {}'.format(os.path.join(dirname, filename)))
@neovim.command('MatlabRename', sync=True, nargs='1')
def rename(self, args):
self.fix_name(args)
@neovim.command('MatlabFixName', sync=True, nargs='*')
def fix_name(self, args):
curr_file = vim_helper.get_current_file_path()
modified = os.path.getmtime(curr_file)
changed = os.path.getctime(curr_file)
head_lines = self.vim.current.buffer[:100]
head_string = '\n'.join(head_lines)
def get_basename_ext(path):
filename = os.path.basename(path)
return os.path.splitext(filename)
def rename_function(name):
new_head = self.function_name_pattern.sub(
r"\1{}\3".format(name), head_string).split('\n')
for i, line in enumerate(new_head):
if line != head_lines[i]:
self.vim.current.buffer[i] = line
basename, ext = get_basename_ext(curr_file)
if len(args) > 0:
new_name, new_ext = get_basename_ext(args[0])
new_ext = new_ext if len(new_ext) > 0 else ext
rename_function(new_name)
self.vim.command("Rename {}{}".format(new_name, new_ext))
return
if vim_helper.is_current_buffer_modified() or changed != modified:
match = self.function_name_pattern.search(head_string)
if match is None:
return
function_name = match.group(2)
self.vim.command("Rename {}{}".format(function_name, ext))
else:
rename_function(basename)
@neovim.autocmd('VimLeave', pattern='*', sync=True)
def vim_leave(self):
if self.gui_controller is not None:
self.gui_controller.close()
@neovim.autocmd('InsertEnter', pattern='*.m')
def insert_enter(self):
self.refresh_buffer()
@neovim.autocmd('BufEnter', pattern='*.m')
def buf_enter(self):
self.set_last_written()
@neovim.autocmd('BufDelete', pattern='*.m')
def buf_delete(self):
path = vim_helper.get_current_file_path()
self.buffer_state.pop(path, None)
@neovim.autocmd('BufWrite', pattern='*.m', sync=True)
def buf_write(self):
self.set_last_written()
self.matlab_write_function_files()
def set_last_written(self):
path = vim_helper.get_current_file_path()
self.buffer_state[path]['last_written'] = time.time()
def refresh_buffer(self):
path = vim_helper.get_current_file_path()
last_written = self.buffer_state[path]['last_written']
if time.time() - last_written < 1 or not os.path.isfile(path):
return
modified = os.stat(path).st_mtime
if modified > last_written + 8:
row_col = vim_helper.get_cursor()
vim_helper.edit_file(path)
vim_helper.set_cursor(row_col)