-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathanalyzer.py
142 lines (108 loc) · 4.15 KB
/
analyzer.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
# Copyright (c) 2014, Guillermo López-Anglada. Please see the AUTHORS file for details.
# All rights reserved. Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.)
import os
import threading
import time
from collections import defaultdict
import sublime
import sublime_plugin
from Dart.sublime_plugin_lib import PluginLogger
from Dart.sublime_plugin_lib.events import IdleIntervalEventListener
from Dart.sublime_plugin_lib.path import is_active
from Dart.sublime_plugin_lib.sublime import after
from Dart.lib.analyzer.analyzer import AnalysisServer
from Dart.lib.error import ConfigError
from Dart.lib.path import is_view_dart_script
from Dart.lib.path import only_for_dart_files
from Dart.lib.pub_package import DartFile
from Dart.lib.sdk import SDK
_logger = PluginLogger(__name__)
# TODO: move this to _init_.py together with other singletons?
g_server = None # singleton
def plugin_loaded():
sdk = SDK()
if not sdk.enable_analysis_server:
return
try:
sdk.check_for_critical_configuration_errors()
except ConfigError as e:
print("Dart: " + str(e))
_logger.error(e)
return
# FIXME(guillermooo): Ignoring, then de-ignoring this package throws
# errors. (Potential ST3 bug: https://github.com/SublimeTextIssues/Core/issues/386)
# Make ST more responsive on startup.
sublime.set_timeout(_init, 500)
def _init():
global g_server
_logger.info('starting dart analyzer')
try:
g_server = AnalysisServer()
threading.Thread(target=g_server.start).run()
except Exception as e:
print('Dart: Exception occurred during init. Aborting')
print('==============================================')
print(e)
print('==============================================')
return
# print('Dart: Starting analysis server...')
def plugin_unloaded():
# The worker threads handling requests/responses block when reading their
# queue, so give them something.
# XXX: This handler loads at times I wouldn't expect it to and ends up
# killing the plugin. Disable this for now.
# g_server.stop()
pass
class DartIdleTimeMoninor(IdleIntervalEventListener):
"""
After ST has been idle for an interval, sends new content to the analysis
server if needed.
"""
def __init__(self, *args, **kwargs):
super().__init__(duration=1200)
def check(self, view):
return DartFile(view).is_dart_file
def on_idle(self, view):
if not AnalysisServer.ping():
return
if view.is_dirty() and is_active(view):
_logger.debug('sending overlay data for %s', view.file_name())
g_server.send_add_content(view)
class DartViewEventsMonitor(sublime_plugin.EventListener):
"""
Monitors a range of events raised by views.
"""
@only_for_dart_files
def on_load(self, view):
if AnalysisServer.ping():
g_server.send_remove_content(view)
# TODO(guillermooo): Use on_modified_async
@only_for_dart_files
def on_modified(self, view):
# if we've `revert`ed the buffer, it'll be clean
if not view.is_dirty():
self.on_load(view)
return
@only_for_dart_files
def on_post_save(self, view):
# The file has been saved, so force use of filesystem content.
if AnalysisServer.ping():
g_server.send_remove_content(view)
@only_for_dart_files
def on_deactivated(self, view):
# FIXME: what's this supposed to do?
# Perhaps we should remove this file from the priority files?
if not is_view_dart_script(view):
return
@only_for_dart_files
def on_activated(self, view):
if AnalysisServer.ping() and not view.is_loading():
g_server.add_root(view, view.file_name())
if is_active(view):
g_server.send_set_priority_files(view, [view.file_name()])
if view.is_dirty():
g_server.send_add_content(view)
else:
# XXX: Retry this a limited amount of times and increase timeout?
after(250, self.on_activated, view)