-
Notifications
You must be signed in to change notification settings - Fork 4
/
ReaSyntax.py
178 lines (138 loc) · 6.76 KB
/
ReaSyntax.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
import sublime, sublime_plugin
import ntpath
import os
# ---------------------------------------------------------------------------------------------------------------------
PACKAGE_SETTINGS = "ReaSyntax.sublime-settings"
JS_SETTINGS = "ReaSyntax - JS.sublime-settings"
EEL_SETTINGS = "ReaSyntax - EEL.sublime-settings"
WALTER_SETTINGS = "ReaSyntax - WALTER.sublime-settings"
JS_SYNTAX = "Packages/ReaSyntax/ReaSyntax - JS.tmLanguage"
LUA_COMPLETIONS = "ReaSyntax - Lua completions.sublime-settings"
#PY_COMPLETIONS = "ReaSyntax - Python completions.sublime-settings"
# ---------------------------------------------------------------------------------------------------------------------
class ReaSyntax:
detectJs = False
luaScriptsPath = None
luaCompletions = []
#pythonScriptsPath = None
#pythonCompletions = []
lastUsedCompletions = []
lastCheckedCompletionsFile = None
@classmethod
def Init (cls):
# Load package settings
cls.settings = sublime.load_settings(PACKAGE_SETTINGS)
cls.settings.add_on_change(PACKAGE_SETTINGS, cls.SettingsChange)
# Load specific settings for Lua
cls.settingsLua = sublime.load_settings(LUA_COMPLETIONS)
cls.settingsLua.add_on_change(LUA_COMPLETIONS, cls.SettingsChange)
# Load specific settings for Python
# cls.settingsPy = sublime.load_settings(PY_COMPLETIONS)
# cls.settingsPy.add_on_change(PY_COMPLETIONS, cls.SettingsChange)
# Apply current settings
cls.SettingsChange()
@classmethod
def SettingsChange (cls):
# Reset cached completions data
ReaSyntax.lastUsedCompletions = []
ReaSyntax.lastCheckedCompletionsFile = None
# Load general package settings
ReaSyntax.detectJs = cls.settings.get("detect_js_file", True)
# Load Lua completions and paths for files which should have auto completions
# ReaSyntax.luaScriptsPath = cls.settings.get("lua_scripts_folders", None)
# ReaSyntax.luaScriptsPath = None if (ReaSyntax.luaScriptsPath is not None and len(ReaSyntax.luaScriptsPath) <= 0) else ReaSyntax.luaScriptsPath
# del ReaSyntax.luaCompletions[:]
# if ReaSyntax.luaScriptsPath is not None:
# ReaSyntax.luaScriptsPath = [""] if ("" in ReaSyntax.luaScriptsPath) else ReaSyntax.luaScriptsPath
# ReaSyntax.luaCompletions = cls.settingsLua.get("completions", [])
# Load Python completions and paths for files which should have auto completions
#ReaSyntax.pythonScriptsPath = cls.settings.get("python_scripts_folders", None)
#ReaSyntax.pythonScriptsPath = None if (ReaSyntax.pythonScriptsPath is not None and len(ReaSyntax.pythonScriptsPath) <= 0) else ReaSyntax.pythonScriptsPath
#del ReaSyntax.pythonCompletions[:]
#if ReaSyntax.pythonScriptsPath is not None:
# ReaSyntax.pythonScriptsPath = [""] if ("" in ReaSyntax.pythonScriptsPath) else ReaSyntax.pythonScriptsPath
# ReaSyntax.pythonCompletions = cls.settingsLua.get("completions", [])
# Set color scheme for each syntax
for i in range(3):
syntax = (JS_SETTINGS if (i == 0) else (EEL_SETTINGS if (i == 1) else WALTER_SETTINGS))
key = ("color_scheme_js" if (i == 0) else ("color_scheme_eel" if (i == 1) else "color_scheme_walter"))
colorScheme = cls.settings.get(key)
if (colorScheme):
sublime.load_settings(syntax).set("color_scheme", colorScheme)
else:
sublime.load_settings(syntax).erase("color_scheme")
sublime.save_settings(syntax)
# @classmethod
# def GetCompletions (cls, filePath):
# completions = []
# # We cache last requested file path and completion file to skip checks for every call
# if ReaSyntax.lastCheckedCompletionsFile is not None and ReaSyntax.lastCheckedCompletionsFile == filePath:
# completions = ReaSyntax.lastUsedCompletions
# else:
# # Save this file path as last checked before converting it to real path
# ReaSyntax.lastCheckedCompletionsFile = filePath
# filePath = os.path.realpath(filePath)
# # Get correct path and completions list for current file type
# fileType = ntpath.splitext(filePath)[1]
# pathsList = None
# if fileType == ".lua": pathsList = ReaSyntax.luaScriptsPath; completions = ReaSyntax.luaCompletions
# #elif fileType == ".py": pathsList = ReaSyntax.pythonScriptsPath; completions = ReaSyntax.pythonCompletions
# # Search for current file path in list of allowable paths
# pathFound = False
# if pathsList is not None:
# if ("" in pathsList):
# pathFound = True
# else:
# for path in pathsList:
# currentPath = os.path.join(os.path.realpath(path), '')
# if os.path.commonprefix([currentPath, filePath]) == currentPath:
# pathFound = True
# break
# if not pathFound:
# completions = []
# # Cache last detected completions
# ReaSyntax.lastUsedCompletions = completions
# return completions
# ---------------------------------------------------------------------------------------------------------------------
class EventDump (sublime_plugin.EventListener):
def on_load (self, view):
if ReaSyntax.detectJs and ntpath.splitext(view.file_name())[1] == "":
jsFile = False
descPos = view.find("^\\s*desc:", 0)
# Found "desc:" line, check if it's the first line in the file (omitting comments and empty lines)
if not descPos.empty():
if descPos.begin() == 0:
jsFile = True
else:
lastComment = view.find("(^\\s*/\\*([^*]|[\r\n]|(\\*+([^*/]|[\r\n])))*\\*+/)|(^\\s*//.*)|(^\\s*$)", 0)
if not lastComment.empty() and lastComment.begin() == 0:
while lastComment.end() < descPos.begin():
if (lastComment.end() + 1 == descPos.begin()):
jsFile = True
break
comment = view.find("(/^\\s*\\*([^*]|[\r\n]|(\\*+([^*/]|[\r\n])))*\\*+/)|(^\\s*//.*)|(^\\s*$)", lastComment.end())
if lastComment.end() + 1 != comment.begin(): # There's something between comments, abort
break
if (comment.empty()): # Reached last comment before "desc:"
break
lastComment = comment
# No match yet, try to find at least 2 code sections
if not jsFile:
codeSections = list()
view.find_all("^@(init|slider|block|sample|serialize|gfx)", 0, "\\1", codeSections)
# Make sure there isn't more than one code section per type
if len(codeSections) == len(set(codeSections)) and len(codeSections) >= 2:
view.set_syntax_file(JS_SYNTAX)
if jsFile:
view.set_syntax_file(JS_SYNTAX)
# def on_query_completions (self, view, prefix, locations):
# region = view.line(locations[0])
# region.b = locations[0]
# print(view.substr(region))
# return ReaSyntax.GetCompletions(view.file_name())
# ---------------------------------------------------------------------------------------------------------------------
def plugin_loaded():
ReaSyntax.Init()
st = 3000 if sublime.version() == '' else int(sublime.version())
if st < 3000:
plugin_loaded() # won't get called by ST2 automatically so do it manually