Skip to content

Commit f9d25c4

Browse files
committed
fixes
1 parent 78682b1 commit f9d25c4

File tree

1 file changed

+91
-75
lines changed

1 file changed

+91
-75
lines changed

fold_angular_imports.py

Lines changed: 91 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,19 @@ def on_load_async(self, view):
2121
if self.should_auto_fold(view):
2222
self.fold_angular_imports(view)
2323

24-
def on_activated_async(self, view):
25-
"""Called when a view gains input focus"""
26-
# Only fold if not already processed and auto-folding is enabled
27-
if self.should_auto_fold(view) and not view.settings().get("angular_imports_folded", False):
28-
self.fold_angular_imports(view)
24+
# Removed on_activated_async to prevent auto-folding when view regains focus
25+
# This was causing issues where manually unfolding would immediately re-fold
26+
# Auto-folding now only happens on file load via on_load_async
2927

3028
def should_auto_fold(self, view):
3129
"""Check if auto-folding should be enabled for this view"""
3230
# Check package settings for auto-folding preference
3331
settings = sublime.load_settings("NgxHTML.sublime-settings")
3432
auto_fold_enabled = settings.get("auto_fold_angular_imports", False)
35-
33+
3634
if not auto_fold_enabled:
3735
return False
38-
36+
3937
# Check if it's a TypeScript file
4038
return self.is_typescript_file(view)
4139

@@ -57,15 +55,15 @@ def fold_angular_imports(self, view):
5755
view.fold(imports_region)
5856
view.settings().set("import_arrays_folded", True)
5957
folded_something = True
60-
58+
6159
# Find and fold import statement chunks at the top (for all TypeScript files)
6260
import_chunks = self.find_import_chunks(view, file_content)
6361
if import_chunks:
6462
for chunk in import_chunks:
6563
view.fold(chunk)
6664
view.settings().set("import_statements_folded", True)
6765
folded_something = True
68-
66+
6967
if folded_something:
7068
view.settings().set("angular_imports_folded", True)
7169

@@ -91,7 +89,7 @@ def find_imports_array(self, view, content):
9189

9290
# Get the position of the opening bracket
9391
bracket_start = match.end() - 1 # Position of the opening bracket '['
94-
92+
9593
# Find the matching closing bracket
9694
bracket_count = 0
9795
pos = bracket_start # Start from the opening bracket
@@ -107,7 +105,7 @@ def find_imports_array(self, view, content):
107105
# Fold from after the opening bracket to before the closing bracket
108106
fold_start = bracket_start + 1 # After the '['
109107
fold_end = pos # Before the ']'
110-
108+
111109
# Only fold if there's content between brackets
112110
if fold_end > fold_start:
113111
return sublime.Region(fold_start, fold_end)
@@ -119,135 +117,153 @@ def find_imports_array(self, view, content):
119117

120118
def find_import_chunks(self, view, content):
121119
"""Find chunks of import statements and return regions for folding"""
122-
lines = content.split('\n')
120+
lines = content.split("\n")
123121
chunks = []
124122
current_chunk_start = None
125123
current_chunk_end = None
126124
in_multiline_import = False
127-
125+
128126
i = 0
129127
while i < len(lines):
130128
line = lines[i]
131129
stripped_line = line.strip()
132-
130+
133131
# Check if this is a single-line import
134-
is_single_line_import = (stripped_line.startswith('import ') and
135-
' from ' in stripped_line and
136-
not stripped_line.startswith('//') and
137-
not stripped_line.startswith('/*'))
138-
132+
is_single_line_import = (
133+
stripped_line.startswith("import ")
134+
and " from " in stripped_line
135+
and not stripped_line.startswith("//")
136+
and not stripped_line.startswith("/*")
137+
)
138+
139139
# Check if this starts a multi-line import
140-
is_multiline_import_start = (stripped_line.startswith('import ') and
141-
' from ' not in stripped_line and
142-
'{' in stripped_line and
143-
not stripped_line.startswith('//') and
144-
not stripped_line.startswith('/*'))
145-
140+
is_multiline_import_start = (
141+
stripped_line.startswith("import ")
142+
and " from " not in stripped_line
143+
and "{" in stripped_line
144+
and not stripped_line.startswith("//")
145+
and not stripped_line.startswith("/*")
146+
)
147+
146148
# Check if we're continuing a multi-line import
147149
is_multiline_import_continuation = in_multiline_import
148-
150+
149151
# Check if this ends a multi-line import
150-
is_multiline_import_end = in_multiline_import and ' from ' in stripped_line
151-
152-
is_import_line = (is_single_line_import or
153-
is_multiline_import_start or
154-
is_multiline_import_continuation or
155-
is_multiline_import_end)
156-
152+
is_multiline_import_end = in_multiline_import and " from " in stripped_line
153+
154+
is_import_line = (
155+
is_single_line_import
156+
or is_multiline_import_start
157+
or is_multiline_import_continuation
158+
or is_multiline_import_end
159+
)
160+
157161
# Update multiline import state
158162
if is_multiline_import_start:
159163
in_multiline_import = True
160164
elif is_multiline_import_end:
161165
in_multiline_import = False
162-
166+
163167
if is_import_line:
164168
if current_chunk_start is None:
165169
# Start of a new chunk
166170
current_chunk_start = i
167171
current_chunk_end = i
168172
else:
169173
# Check if this is a blank line or comment that could separate chunks
170-
is_separator = (stripped_line == '' or
171-
stripped_line.startswith('//') or
172-
stripped_line.startswith('/*') or
173-
stripped_line.startswith('*'))
174-
174+
is_separator = (
175+
stripped_line == ""
176+
or stripped_line.startswith("//")
177+
or stripped_line.startswith("/*")
178+
or stripped_line.startswith("*")
179+
)
180+
175181
if current_chunk_start is not None and not is_separator:
176182
# End of current chunk - create fold if chunk has multiple lines
177183
if current_chunk_end > current_chunk_start:
178-
chunk_region = self.lines_to_region(view, content, current_chunk_start, current_chunk_end)
184+
chunk_region = self.lines_to_region(
185+
view, content, current_chunk_start, current_chunk_end
186+
)
179187
if chunk_region:
180188
chunks.append(chunk_region)
181189
current_chunk_start = None
182190
current_chunk_end = None
183-
191+
184192
# Stop if we hit substantial non-import content (not just separators)
185193
if not is_separator:
186194
break
187195
elif current_chunk_start is not None and is_separator:
188196
# We're in a chunk and hit a separator - end current chunk and prepare for next
189197
if current_chunk_end > current_chunk_start:
190-
chunk_region = self.lines_to_region(view, content, current_chunk_start, current_chunk_end)
198+
chunk_region = self.lines_to_region(
199+
view, content, current_chunk_start, current_chunk_end
200+
)
191201
if chunk_region:
192202
chunks.append(chunk_region)
193203
current_chunk_start = None
194204
current_chunk_end = None
195-
205+
196206
i += 1
197-
207+
198208
# Handle case where file ends with imports
199209
if current_chunk_start is not None and current_chunk_end > current_chunk_start:
200-
chunk_region = self.lines_to_region(view, content, current_chunk_start, current_chunk_end)
210+
chunk_region = self.lines_to_region(
211+
view, content, current_chunk_start, current_chunk_end
212+
)
201213
if chunk_region:
202214
chunks.append(chunk_region)
203-
215+
204216
return chunks
205217

206218
def lines_to_region(self, view, content, start_line, end_line):
207219
"""Convert line numbers to a Sublime Text region, leaving first import visible"""
208-
lines = content.split('\n')
209-
220+
lines = content.split("\n")
221+
210222
# Only fold if there's more than one import (need at least 2 lines to fold)
211223
if end_line <= start_line:
212224
return None
213-
225+
214226
# Find where the first import statement ends
215227
first_import_end = start_line
216228
in_multiline_import = False
217-
229+
218230
for i in range(start_line, end_line + 1):
219231
line = lines[i].strip()
220-
232+
221233
# Check if this starts a multi-line import
222-
if line.startswith('import ') and '{' in line and ' from ' not in line:
234+
if line.startswith("import ") and "{" in line and " from " not in line:
223235
in_multiline_import = True
224-
236+
225237
# If we're in a multi-line import, keep going until we find the 'from' line
226238
if in_multiline_import:
227-
if ' from ' in line:
239+
if " from " in line:
228240
first_import_end = i
229241
break
230242
else:
231243
# Single line import - this line is the complete import
232-
if line.startswith('import ') and ' from ' in line:
244+
if line.startswith("import ") and " from " in line:
233245
first_import_end = i
234246
break
235-
247+
236248
# Start folding from the line after the first complete import
237249
fold_start_line = first_import_end + 1
238-
250+
239251
# Make sure we have something to fold
240252
if fold_start_line > end_line:
241253
return None
242-
254+
243255
# Calculate character positions
244-
fold_start_pos = sum(len(lines[i]) + 1 for i in range(fold_start_line)) # +1 for newline
245-
fold_end_pos = sum(len(lines[i]) + 1 for i in range(end_line + 1)) - 1 # -1 to not include final newline
246-
256+
fold_start_pos = sum(
257+
len(lines[i]) + 1 for i in range(fold_start_line)
258+
) # +1 for newline
259+
fold_end_pos = (
260+
sum(len(lines[i]) + 1 for i in range(end_line + 1)) - 1
261+
) # -1 to not include final newline
262+
247263
# Only fold if there's actual content
248264
if fold_end_pos > fold_start_pos:
249265
return sublime.Region(fold_start_pos, fold_end_pos)
250-
266+
251267
return None
252268

253269

@@ -260,21 +276,21 @@ def run(self, edit):
260276

261277
content = self.view.substr(sublime.Region(0, self.view.size()))
262278
listener = FoldAngularImportsListener()
263-
279+
264280
# Find and fold import statement chunks only
265281
import_chunks = listener.find_import_chunks(self.view, content)
266282
for chunk in import_chunks:
267283
self.view.fold(chunk)
268-
284+
269285
if import_chunks:
270286
self.view.settings().set("import_statements_folded", True)
271287

272288
def is_enabled(self):
273-
return self.is_typescript_file()
274-
289+
return bool(self.is_typescript_file())
290+
275291
def is_typescript_file(self):
276292
file_name = self.view.file_name()
277-
return file_name and file_name.endswith(".ts")
293+
return bool(file_name and file_name.endswith(".ts"))
278294

279295

280296
class FoldImportArraysCommand(sublime_plugin.TextCommand):
@@ -286,16 +302,16 @@ def run(self, edit):
286302

287303
content = self.view.substr(sublime.Region(0, self.view.size()))
288304
listener = FoldAngularImportsListener()
289-
305+
290306
# Find and fold the imports array only
291307
imports_region = listener.find_imports_array(self.view, content)
292308
if imports_region:
293309
self.view.fold(imports_region)
294310
self.view.settings().set("import_arrays_folded", True)
295311

296312
def is_enabled(self):
297-
return self.is_angular_file()
298-
313+
return bool(self.is_angular_file())
314+
299315
def is_angular_file(self):
300316
file_name = self.view.file_name()
301317
if not file_name or not file_name.endswith(".ts"):
@@ -314,7 +330,7 @@ def run(self, edit):
314330

315331
def is_enabled(self):
316332
file_name = self.view.file_name()
317-
return file_name and file_name.endswith(".ts")
333+
return bool(file_name and file_name.endswith(".ts"))
318334

319335

320336
class UnfoldImportStatementsCommand(sublime_plugin.TextCommand):
@@ -325,7 +341,7 @@ def run(self, edit):
325341
# In practice, this will unfold everything, but we'll track the state
326342
self.view.unfold(sublime.Region(0, self.view.size()))
327343
self.view.settings().set("import_statements_folded", False)
328-
344+
329345
# Re-fold import arrays if they were previously folded
330346
if self.view.settings().get("import_arrays_folded", False):
331347
content = self.view.substr(sublime.Region(0, self.view.size()))
@@ -336,7 +352,7 @@ def run(self, edit):
336352

337353
def is_enabled(self):
338354
file_name = self.view.file_name()
339-
return file_name and file_name.endswith(".ts")
355+
return bool(file_name and file_name.endswith(".ts"))
340356

341357

342358
class UnfoldImportArraysCommand(sublime_plugin.TextCommand):
@@ -346,7 +362,7 @@ def run(self, edit):
346362
# Unfold all regions, but this is a limitation - Sublime doesn't allow selective unfolding
347363
self.view.unfold(sublime.Region(0, self.view.size()))
348364
self.view.settings().set("import_arrays_folded", False)
349-
365+
350366
# Re-fold import statements if they were previously folded
351367
if self.view.settings().get("import_statements_folded", False):
352368
content = self.view.substr(sublime.Region(0, self.view.size()))
@@ -376,4 +392,4 @@ def run(self, edit):
376392

377393
def is_enabled(self):
378394
file_name = self.view.file_name()
379-
return file_name and file_name.endswith(".ts")
395+
return bool(file_name and file_name.endswith(".ts"))

0 commit comments

Comments
 (0)