This repository was archived by the owner on Aug 4, 2022. It is now read-only.
forked from SublimeHaskell/SublimeHaskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbols.py
873 lines (683 loc) · 27.8 KB
/
symbols.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
# -*- coding: UTF-8 -*-
from functools import total_ordering, reduce
import html
import json
import os.path
import re
import sublime
import SublimeHaskell.internals.unicode_opers as UnicodeOpers
@total_ordering
class Position(object):
def __init__(self, line, column):
self.line = line
self.column = column
def __str__(self):
return self.to_string()
def __unicode__(self):
return self.to_string()
def to_string(self):
if self.column is None:
return str(self.line)
return u':'.join([str(self.line), str(self.column)])
def __eq__(self, other):
return self.line == other.line and self.column == other.column
def __lt__(self, other):
return self.line < other.line or (self.line == other.line and self.column < other.column)
def to_zero_based(self):
self.line = self.line - 1
self.column = self.column - 1
return self
def from_zero_based(self):
self.line = self.line + 1
self.column = self.column + 1
return self
def to_point(self, view):
return view.text_point(self.line, self.column)
@staticmethod
def from_point(view, point):
(line, col) = view.rowcol(point)
return Position(int(line), int(col))
@staticmethod
def from_str(pt_str):
comps = pt_str.split(':')
if len(comps) == 1:
return Position(int(comps[0]), 0)
elif len(comps) == 2:
return Position(int(comps[0]), int(comps[1]))
return None
@total_ordering
class Region(object):
def __init__(self, start, end=None, view=None, region_key=None):
self.start = start
self.end = end or self.start
self.view = view
self.region_key = region_key
def __str__(self):
return self.to_string()
def __unicode__(self):
return self.to_string()
def to_string(self):
return u'-'.join([str(self.start), str(self.end)])
def __eq__(self, other):
return self.start == other.start and self.end == other.end
def __lt__(self, other):
return self.start < other.start or (self.start == other.start and self.end < other.end)
def from_zero_based(self):
self.start.from_zero_based()
self.end.from_zero_based()
return self
def to_zero_based(self):
self.start.to_zero_based()
self.end.to_zero_based()
return self
def to_region(self, view=None):
if not view:
view = self.view
if not view:
raise RuntimeError('symbols.Region: view is None')
return sublime.Region(self.start.to_point(view), self.end.to_point(view))
def update(self):
if self.saved():
rgns = self.view.get_regions(self.region_key)
if rgns:
rgn = Region.from_region(self.view, rgns[0], self.region_key)
self.start = rgn.start
self.end = rgn.end
def save(self, view, region_key):
self.view = view
self.region_key = region_key
def saved(self):
return self.view and self.region_key
def erase(self):
if self.saved():
self.view.erase_regions(self.region_key)
self.view = None
self.region_key = None
@staticmethod
def from_region(view, rgn, region_key=None):
return Region(Position.from_point(view, rgn.begin()),
Position.from_point(view, rgn.end()),
view if region_key else None,
region_key)
@staticmethod
def from_str(rgn_str):
comps = rgn_str.split('-')
if len(comps) == 1:
return Region(Position.from_str(comps[0]))
elif len(comps) == 2:
return Region(Position.from_str(comps[0]), Position.from_str(comps[1]))
return None
def empty(self):
return self.start == self.end
class Location(object):
"""
Location in file at line
"""
def __init__(self, filename, project=None):
self.project = project
self.filename = filename
def __str__(self):
return self.to_string()
def to_string(self):
return self.filename
def is_null(self):
return self.project is None and self.filename is None
def get_id(self):
return self.filename
def source_location(loc, pos):
""" Returns filename:line:column """
if not pos:
return str(loc)
return ':'.join([str(loc), str(pos)])
class Package(object):
def __init__(self, name, version=None):
self.name = name
self.version = version
def package_id(self):
return '{0}-{1}'.format(self.name, self.version) if self.version is not None else self.name
def parse_package(package_id):
if package_id is None:
return None
pkg = re.match(r'([\w\-]+)\-([\d\.]+)', package_id)
if pkg:
(name, version) = pkg.groups()
return Package(name, version)
pkg = re.match(r'([\w\-]+)', package_id)
if pkg:
return Package(pkg.groups()[0])
return None
class PackageDb(object):
def __init__(self, global_db=False, user_db=False, package_db=None):
self.global_db = False
self.user_db = False
self.package_db = None
if global_db:
self.global_db = True
return
if user_db:
self.user_db = True
return
if package_db:
self.package_db = package_db
return
def __str__(self):
return self.to_string()
def to_string(self):
if self.global_db:
return 'global-db'
if self.user_db:
return 'user-db'
if self.package_db:
return self.package_db
@staticmethod
def from_string(pkgdb_str):
if pkgdb_str == 'global-db':
return PackageDb(global_db=True)
if pkgdb_str == 'user-db':
return PackageDb(user_db=True)
return PackageDb(package_db=pkgdb_str)
class InstalledLocation(object):
"""
Module location in cabal
"""
def __init__(self, package, db=PackageDb(global_db=True)):
self.package = package
self.db = db
def __str__(self):
return self.to_string()
def to_string(self):
return '{0} in {1}'.format(self.package.package_id(), self.db.to_string())
def is_null(self):
return self.package is None
def get_id(self):
return '{0}:{1}'.format(self.db.to_string(), self.package.package_id())
def is_cabal(self):
return not self.db.package_db
def sandbox(self):
return self.db.package_db
class OtherLocation(object):
"""
Other module location
"""
def __init__(self, source):
self.source = source
def __str__(self):
return self.to_string()
def to_string(self):
return self.source or ""
def is_null(self):
return self.source is None
def get_id(self):
return '[{0}]'.format(self.source)
def location_package_name(loc):
if isinstance(loc, InstalledLocation) and loc.package:
return loc.package.name
return None
def location_project(loc):
if isinstance(loc, Location) and loc.project:
return loc.project
return None
def location_cabal(loc):
if isinstance(loc, InstalledLocation):
return loc.cabal
return None
class Symbol(object):
"""
Haskell symbol: module, function, data, class etc.
"""
def __init__(self, symbol_type, name):
self.what = symbol_type
self.name = name
self.tags = {}
def __str__(self):
return u'Symbol({0} \'{1}\' {2})'.format(self.what, self.name, self.tags)
class Import(object):
"""
Haskell import of module
"""
def __init__(self, module_name, is_qualified=False, import_as=None, position=None, location=None):
self.module = module_name
self.is_qualified = is_qualified
self.import_as = import_as
self.position = position
self.location = location
def __repr__(self):
return u'Import({0}{1}{2})'.format(self.module, ' qualified' if self.is_qualified else '',
' as ' + self.import_as if self.import_as else '')
def dump(self):
return self.__dict__
def module_location(filename):
return Location(filename)
class Module(Symbol):
"""
Haskell module symbol
"""
def __init__(self, module_name, exports=None, imports=None, declarations=None, location=None, last_inspection_time=0):
super().__init__('module', module_name)
self.location = location
# List of strings
self.exports = exports[:] if exports is not None else []
# Dictionary from module name to Import object
self.imports = imports[:] if imports is not None else []
for i in self.imports:
i.location = self.location
# Dictionary from name to Symbol
self.declarations = declarations.copy() if declarations else {}
for decl in self.declarations.values():
decl.location = self.location
decl.module = self
# Time as from time.time()
self.last_inspection_time = last_inspection_time
def __repr__(self):
return u'Module({0} (exports {1}, imports {2} decls {3}))'.format(self.name, len(self.exports), len(self.imports),
len(self.declarations))
def add_declaration(self, new_declaration):
if not new_declaration.module:
new_declaration.module = self
new_declaration.location = self.location
if new_declaration.module != self:
raise RuntimeError("Adding declaration to other module")
self.declarations[new_declaration.name] = new_declaration
def unalias(self, module_alias):
"""
Unalias module import if any
Returns list of unaliased modules
"""
return [i.module for i in self.imports if i.import_as == module_alias]
def get_location_id(self):
if isinstance(self.location, InstalledLocation):
return '{0}:{1}'.format(self.location.get_id(), self.name)
return self.location.get_id()
def by_source(self):
return isinstance(self.location, Location)
def by_cabal(self):
return isinstance(self.location, InstalledLocation)
def by_hayoo(self):
return isinstance(self.location, OtherLocation)
def escape_text(txt):
"""
Escape text, replacing leading spaces to non-breaking ones and newlines to <br> tag
"""
lines = []
for line in txt.splitlines():
lead_spaces = re.match(r'^\s+', line)
if lead_spaces: # Replace leading spaces with non-breaking ones
lines.append(lead_spaces.end() * ' ' + html.escape(line[lead_spaces.end():], quote=False))
else:
lines.append(html.escape(line, quote=False))
return '<br>'.join(lines)
def unicode_operators(wrap_fn):
def wrapped(*args, use_unicode=None, **kwargs):
if use_unicode is False:
return wrap_fn(*args, **kwargs)
return UnicodeOpers.use_unicode_operators(wrap_fn(*args, **kwargs), force=(use_unicode is True))
return wrapped
class Declaration(Symbol):
def __init__(self, name, decl_type='declaration', docs=None, imported=None, defined=None, position=None, module=None):
super().__init__(decl_type, name)
self.docs = docs
self.imported = imported[:] if imported else []
self.defined = defined
self.position = position
self.module = module
def __repr__(self):
return u'Declaration({0} {1} [{2}])'.format(self.what, self.name, self.module)
def defined_module(self):
return self.defined or self.module
def by_source(self):
return isinstance(self.defined_module().location, Location)
def by_cabal(self):
return isinstance(self.defined_module().location, InstalledLocation)
def by_hayoo(self):
return isinstance(self.defined_module().location, OtherLocation)
def has_source_location(self):
return self.by_source() and self.position is not None
def get_source_location(self):
if self.has_source_location():
return source_location(self.defined_module().location, self.position)
return None
def make_qualified(self):
self.name = self.qualified_name()
def module_name(self):
if self.imported:
return self.imported[0].module
return self.module.name
def imported_names(self):
return sorted(list(set([i.module for i in self.imported or []])))
def imported_from_name(self):
inames = self.imported_names()
return inames[0] if inames else ''
def suggest(self):
""" Returns suggestion for this declaration """
return ('{0}\t{1}'.format(self.name, self.imported_from_name()), self.name)
def brief(self, _short=False):
""" Brief information, just a name by default """
return self.name
def qualified_name(self):
return '.'.join([self.module_name(), self.name])
@unicode_operators
def detailed(self):
""" Detailed info for use in Symbol Info command """
parts = [self.brief()]
if self.imported_names():
parts.extend(['', 'Imported from {0}'.format(', '.join(self.imported_names()))])
if self.docs:
parts.extend(['', self.docs])
parts.append('')
if self.by_source():
if self.defined_module().location.project:
parts.append('Project: {0}'.format(self.defined_module().location.project))
elif self.by_cabal():
parts.append('Installed in: {0}'.format(self.defined_module().location.db.to_string()))
parts.append('Package: {0}'.format(self.defined_module().location.package.package_id()))
if self.has_source_location():
parts.append('Defined at: {0}'.format(self.get_source_location()))
else:
parts.append('Defined in: {0}'.format(self.defined_module().name))
return '\n'.join(parts)
@unicode_operators
def popup_brief(self):
""" Brief info on popup with name, possibly with link if have source location """
info = u'<span class="function">{0}</span>'.format(html.escape(self.name, quote=False))
if self.has_source_location():
return u'<a href="{0}">{1}</a>'.format(html.escape(self.get_source_location()), info)
return info
@unicode_operators
def popup(self, comments=None):
""" Full info on popup with docs """
parts = [u'<p>']
parts.append(self.popup_brief())
for cmnt in comments or []:
parts.append(u'<br>{0}<span class="comment">-- {1}</span>'.format(4 * ' ', cmnt))
if self.imported_names():
parts.append(u'<br>{0}<span class="comment">-- Imported from {1}</span>'.format(
4 * ' ',
html.escape(u', '.join(self.imported_names()), quote=False)))
if self.defined_module():
module_ref = html.escape(self.defined_module().name, quote=False)
if self.defined_module().by_source():
module_ref = u'<a href="{0}">{1}</a>'.format(self.defined_module().location.to_string(), module_ref)
elif self.defined_module().by_cabal():
hackage_url = 'http://hackage.haskell.org/package/{0}/docs/{1}.html'.format(
self.defined_module().location.package.package_id(),
self.defined_module().name.replace('.', '-'))
module_ref = u'<a href="{0}">{1}</a>'.format(html.escape(hackage_url), module_ref)
parts.append(u'<br>{0}<span class="comment">-- Defined in {1}</span>'.format(
4 * ' ',
module_ref))
parts.append(u'</p>')
if self.docs:
parts.append(u'<p><span class="docs">{0}</span></p>'.format(escape_text(self.docs)))
# parts.append(u'<a href="info">...</a>')
return u''.join(parts)
def wrap_operator(name):
if re.match(r"[\w']+", name):
return name
return "({0})".format(name)
def format_type(expr):
"""
Format type expression for popup
Set span 'type' for types and 'tyvar' for type variables
"""
if not expr:
return expr
tyname = re.search(r'([a-zA-Z]\w*)|(->|=>|::|\u2192|\u21d2|\u2237)', expr)
if tyname:
tyexpr = expr[tyname.start():tyname.end()]
expr_class = ''
if tyname.group(1):
expr_class = 'type' if tyexpr[0].isupper() else 'tyvar'
elif tyname.group(2):
expr_class = 'operator'
decorated = '<span class="{0}">{1}</span>'.format(expr_class, html.escape(tyexpr, quote=False))
return html.escape(expr[0:tyname.start()], quote=False) + decorated + format_type(expr[tyname.end():])
else:
return html.escape(expr, quote=False)
class Function(Declaration):
"""
Haskell function declaration
"""
def __init__(self, name, function_type, docs=None, imported=None, defined=None, position=None, module=None):
super().__init__(name, 'function', docs, imported or [], defined, position, module)
self.type = function_type
def __repr__(self):
return u'Function({0} :: {1} [{2}])'.format(wrap_operator(self.name), self.type, self.imported_from_name())
def suggest(self):
return (UnicodeOpers.use_unicode_operators(u'{0} :: {1}\t{2}'.format(wrap_operator(self.name),
self.type,
self.imported_from_name())),
self.name)
@unicode_operators
def brief(self, short=False):
if short:
return u'{0}'.format(wrap_operator(self.name))
return u'{0} :: {1}'.format(wrap_operator(self.name), self.type if self.type else u'?')
@unicode_operators
def popup_brief(self):
info = u'<span class="function">{0}</span>'.format(html.escape(self.name, quote=False))
if self.has_source_location():
info = u'<a href="{0}">{1}</a>'.format(html.escape(self.get_source_location()), info)
return u'{0} <span class="operator">::</span> {1}'.format(info, format_type(self.type if self.type else u'?'))
class TypeBase(Declaration):
"""
Haskell type, data or class
"""
def __init__(self, name, decl_type, context, args, definition=None, docs=None, imported=None, defined=None,
position=None, module=None):
super().__init__(name, decl_type, docs, imported or [], defined, position, module)
self.context = context
self.args = args
self.definition = definition
def suggest(self):
return (UnicodeOpers.use_unicode_operators(u'{0} {1}\t{2}'.format(self.name,
' '.join(self.args),
self.imported_from_name())),
self.name)
@unicode_operators
def brief(self, short=False):
if short:
brief_parts = [self.what, self.name]
if self.args:
brief_parts.extend(self.args)
return u' '.join(brief_parts)
if self.definition:
return self.definition
brief_parts = [self.what]
if self.context:
if len(self.context) == 1:
brief_parts.append(u'{0} =>'.format(self.context[0]))
else:
brief_parts.append(u'({0}) =>'.format(', '.join(self.context)))
brief_parts.append(self.name)
if self.args:
brief_parts.append(u' '.join(self.args))
return u' '.join(brief_parts)
@unicode_operators
def popup_brief(self):
parts = [u'<span class="keyword">{0}</span>'.format(html.escape(self.what, quote=False))]
if self.context:
ctx = self.context.split(', ')
if len(ctx) == 1:
parts.append(format_type(u'{0} =>'.format(ctx[0])))
else:
parts.append(format_type(u'({0}) =>'.format(', '.join(ctx))))
name_part = u'<span class="type">{0}</span>'.format(html.escape(self.name, quote=False))
if self.has_source_location():
name_part = u'<a href="{0}">{1}</a>'.format(html.escape(self.get_source_location()), name_part)
parts.append(name_part)
if self.args:
parts.append(format_type(u' '.join(self.args)))
return u' '.join(parts)
class Type(TypeBase):
"""
Haskell type synonym
"""
def __init__(self, name, context, args, definition=None, docs=None, imported=None, defined=None,
position=None, module=None):
super().__init__(name, 'type', context, args, definition, docs, imported or [], defined, position, module)
class Newtype(TypeBase):
"""
Haskell newtype synonym
"""
def __init__(self, name, context, args, definition=None, docs=None, imported=None, defined=None,
position=None, module=None):
super().__init__(name, 'newtype', context, args, definition, docs, imported or [], defined, position, module)
class Data(TypeBase):
"""
Haskell data declaration
"""
def __init__(self, name, context, args, definition=None, docs=None, imported=None, defined=None,
position=None, module=None):
super().__init__(name, 'data', context, args, definition, docs, imported or [], defined, position, module)
class Class(TypeBase):
"""
Haskell class declaration
"""
def __init__(self, name, context, args, definition=None, docs=None, imported=None, defined=None,
position=None, module=None):
super().__init__(name, 'class', context, args, definition, docs, imported or [], defined, position, module)
def __repr__(self):
return u'Class({0})'.format(self.name)
def update_with(left, right, default_value, upd_fn):
"""
unionWith for Python, but modifying first dictionary instead of returning result
"""
for k, val in right.items():
if k not in left:
left[k] = default_value[:]
left[k] = upd_fn(left[k], val)
return left
def same_module(left, right):
"""
Returns true if l is same module as r, which is when module name is equal
and modules defined in one file, in same cabal-dev sandbox or in cabal
"""
same_cabal = left.cabal and right.cabal and (left.cabal == right.cabal)
same_filename = left.location and right.location and (left.location.filename == right.location.filename)
nowhere = (not left.cabal) and (not left.location) and (not right.cabal) and (not right.location)
return left.name == right.name and (same_cabal or same_filename or nowhere)
def same_declaration(left, right):
"""
Returns true if l is same declaration as r
"""
same_mod = left.module and right.module and same_module(left.module, right.module)
nowhere = (not left.module) and (not right.module)
return left.name == right.name and (same_mod or nowhere)
def is_within_project(module, project):
"""
Returns whether module defined within project specified
"""
if module.location:
return module.location.project == project
return False
def is_within_cabal(module, cabal=None):
"""
Returns whether module loaded from cabal specified
"""
return cabal is not None and module.cabal == cabal
def is_by_sources(module):
"""
Returns whether module defined by sources
"""
return module.location is not None
def flatten(lsts):
return reduce(lambda l, r: list(l) + list(r), lsts)
class CabalPackage(object):
def __init__(self, name, synopsis, version, installed, homepage, pkg_license):
self.name = name
self.synopsis = synopsis
self.default_version = version
self.installed_versions = installed[:] if installed else []
self.homepage = homepage
self.license = pkg_license
def brief(self):
return self.name
def detailed(self):
info = []
info.append(self.brief())
info.append('')
if self.synopsis:
info.append(self.synopsis)
info.append('')
if self.default_version:
info.append('Last version: ' + self.default_version)
if self.installed_versions:
info.append('Installed versions: ' + ", ".join(self.installed_versions))
if self.homepage:
info.append('Homepage: ' + self.homepage)
if self.license:
info.append('License: ' + self.license)
return '\n'.join(info)
class Corrector(object):
def __init__(self, region, contents):
self.region = region
self.contents = contents
# def __eq__(self, other):
# return self.region == other.region and self.contents == other.contents
def to_region(self, view):
return self.region.to_region(view)
def from_region(self, view, rgn):
self.region = Region.from_region(view, rgn)
def to_json(self):
return json.dumps({self.region.to_string():self.contents})
@staticmethod
def from_json(j):
corrs = json.loads(j)
its = list(corrs.items())
if len(its) == 1:
(rgn, cts) = its[0]
return Corrector(Region.from_str(rgn), cts)
return None
class Correction(object):
def __init__(self, file, level, message, corrector, message_region=None):
self.file = file
self.level = level
self.message = message
# source messages region, used to match corrector and outputmessage
self.message_region = message_region
self.corrector = corrector
def to_region(self, view):
return self.corrector.to_region(view)
def from_region(self, view, rgn):
self.corrector.from_region(view, rgn)
@unicode_operators
def detailed(self):
if self.corrector.contents:
return u'\u2014 {0}\n Why not:\n\n{1}'.format(self.message, self.corrector.contents)
return u'\u2014 {0}'.format(self.message)
@unicode_operators
def popup(self):
arg1 = html.escape(self.message)
arg2 = html.escape("autofix:" + self.corrector.region.to_string())
parts = [u'<span class="message">— {0} (<a href="{1}">{2}</a>)</span>'.format(arg1, arg2, "autofix")]
if self.corrector.contents:
parts.append(u'<br>{0}<span class="{1}">{2}</span>'.format(2 * " ",
html.escape(self.level),
html.escape("Why not: ")))
parts.append(u'<br><br>{0}<span class="code">{1}</span>'.format(4 * " ", html.escape(self.corrector.contents)))
return u''.join(parts)
def mark_corrections(views, corrs):
for view in views:
if view.file_name() is None:
continue
corrs_ = [corr for corr in corrs if os.path.samefile(corr.file, view.file_name())]
view.add_regions('autofix', [corr.to_region(view) for corr in corrs_], 'entity.name.function', 'dot', 0)
# This allow us track region positions even if file modifies
def add_corrections_regions(views, corrs):
for view in views:
if view.file_name() is None:
continue
corrs_ = [corr for corr in corrs if os.path.samefile(corr.file, view.file_name())]
view.add_regions('autofix', [corr.to_region(view) for corr in corrs_], 'autofix', '', sublime.HIDDEN)
# Restore actual region data from view regions
def restore_corrections_regions(views, corrs):
for view in views:
if view.file_name() is None:
continue
corrs_ = [corr for corr in corrs if os.path.samefile(corr.file, view.file_name())]
rgns = view.get_regions('autofix')
if len(rgns) != len(corrs_):
# Something wrong
return
for rgn, corr in zip(rgns, corrs):
corr.corrector.from_region(view, rgn)