Skip to content

Commit d61e844

Browse files
authored
Merge pull request #173 from TerminalFi/refine/copilotIgnore
2 parents f2ab4c8 + 74a8d7b commit d61e844

File tree

7 files changed

+56
-42
lines changed

7 files changed

+56
-42
lines changed

plugin/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
CopilotSignOutCommand,
2020
)
2121
from .listeners import EventListener, ViewEventListener, copilot_ignore_observer
22-
from .utils import CopilotIgnore
22+
from .utils import CopilotIgnore, all_windows
2323

2424
__all__ = (
2525
# ST: core
@@ -51,6 +51,8 @@ def plugin_loaded() -> None:
5151
"""Executed when this plugin is loaded."""
5252
CopilotPlugin.setup()
5353
copilot_ignore_observer.setup()
54+
for window in all_windows():
55+
CopilotIgnore(window).load_patterns()
5456

5557

5658
def plugin_unloaded() -> None:

plugin/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
)
4646
from .ui import ViewCompletionManager, ViewPanelCompletionManager
4747
from .utils import (
48+
CopilotIgnore,
4849
all_views,
4950
debounce,
5051
get_session_setting,
@@ -288,6 +289,12 @@ def update_status_bar_text(self, extra_variables: dict[str, Any] | None = None)
288289
log_warning(f'Invalid "status_text" template: {e}')
289290
session.set_config_status_async(rendered_text)
290291

292+
@classmethod
293+
def should_ignore(cls, view: sublime.View) -> bool:
294+
if not (window := view.window()):
295+
return False
296+
return CopilotIgnore(window).trigger(view)
297+
291298
@notification_handler(NTFY_FEATURE_FLAGS_NOTIFICATION)
292299
def _handle_feature_flags_notification(self, payload: CopilotPayloadFeatureFlagsNotification) -> None:
293300
pass

plugin/commands.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
REQ_SIGN_IN_WITH_GITHUB_TOKEN,
2828
REQ_SIGN_OUT,
2929
)
30-
from .decorators import _must_be_active_view_not_ignored
30+
from .decorators import _must_be_active_view
3131
from .types import (
3232
CopilotPayloadFileStatus,
3333
CopilotPayloadGetVersion,
@@ -110,13 +110,7 @@ def _record_telemetry(
110110

111111
session.send_request(Request(request, payload), lambda _: None)
112112

113-
@_must_be_active_view_not_ignored(failed_return=False)
114-
@_provide_plugin_session(failed_return=False)
115-
def is_enabled(self, plugin: CopilotPlugin, session: Session) -> bool: # type: ignore
116-
return self._can_meet_requirement(session)
117-
118-
119-
class CopilotIgnoreExemptTextCommand(CopilotTextCommand):
113+
@_must_be_active_view(failed_return=False)
120114
@_provide_plugin_session(failed_return=False)
121115
def is_enabled(self, plugin: CopilotPlugin, session: Session) -> bool: # type: ignore
122116
return self._can_meet_requirement(session)
@@ -129,7 +123,7 @@ def is_enabled(self) -> bool:
129123
return self._can_meet_requirement(session)
130124

131125

132-
class CopilotGetVersionCommand(CopilotIgnoreExemptTextCommand):
126+
class CopilotGetVersionCommand(CopilotTextCommand):
133127
requirement = REQUIRE_NOTHING
134128

135129
@_provide_plugin_session()
@@ -288,7 +282,7 @@ def _on_result_check_file_status(self, payload: CopilotPayloadFileStatus) -> Non
288282
status_message("File is {} in session", payload["status"])
289283

290284

291-
class CopilotSignInCommand(CopilotIgnoreExemptTextCommand):
285+
class CopilotSignInCommand(CopilotTextCommand):
292286
requirement = REQUIRE_NOT_SIGN_IN
293287

294288
@_provide_plugin_session()
@@ -327,7 +321,7 @@ def _on_result_sign_in_confirm(self, payload: CopilotPayloadSignInConfirm) -> No
327321
self.view.run_command("copilot_check_status")
328322

329323

330-
class CopilotSignInWithGithubTokenCommand(CopilotIgnoreExemptTextCommand):
324+
class CopilotSignInWithGithubTokenCommand(CopilotTextCommand):
331325
requirement = REQUIRE_NOT_SIGN_IN
332326

333327
@_provide_plugin_session()
@@ -376,7 +370,7 @@ def _on_result_sign_in_confirm(self, payload: CopilotPayloadSignInConfirm) -> No
376370
self.view.run_command("copilot_check_status")
377371

378372

379-
class CopilotSignOutCommand(CopilotIgnoreExemptTextCommand):
373+
class CopilotSignOutCommand(CopilotTextCommand):
380374
requirement = REQUIRE_SIGN_IN
381375

382376
@_provide_plugin_session()

plugin/decorators.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
from .types import T_Callable
55
from .utils import (
6-
CopilotIgnore,
76
is_active_view,
87
)
98

109

11-
def _must_be_active_view_not_ignored(*, failed_return: Any = None) -> Callable[[T_Callable], T_Callable]:
10+
def _must_be_active_view(*, failed_return: Any = None) -> Callable[[T_Callable], T_Callable]:
1211
def decorator(func: T_Callable) -> T_Callable:
1312
@wraps(func)
1413
def wrapped(self: Any, *arg, **kwargs) -> Any:
15-
if is_active_view(self.view) and not CopilotIgnore(self.view.window()).trigger(self.view):
14+
if is_active_view(self.view):
1615
return func(self, *arg, **kwargs)
1716
return failed_return
1817

plugin/listeners.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from watchdog.observers import Observer
1010

1111
from .client import CopilotPlugin
12-
from .decorators import _must_be_active_view_not_ignored
12+
from .decorators import _must_be_active_view
1313
from .ui import ViewCompletionManager, ViewPanelCompletionManager
1414
from .utils import (
1515
CopilotIgnore,
@@ -46,7 +46,7 @@ def _is_saving(self) -> bool:
4646
def _is_saving(self, value: bool) -> None:
4747
set_copilot_view_setting(self.view, "_is_saving", value)
4848

49-
@_must_be_active_view_not_ignored()
49+
@_must_be_active_view()
5050
def on_modified_async(self) -> None:
5151
self._is_modified = True
5252

@@ -60,20 +60,6 @@ def on_modified_async(self) -> None:
6060
if not self._is_saving and get_session_setting(session, "auto_ask_completions") and not vcm.is_waiting:
6161
plugin.request_get_completions(self.view)
6262

63-
def on_activated_async(self) -> None:
64-
if (
65-
(window := self.view.window())
66-
and (plugin := CopilotPlugin.from_view(self.view))
67-
and copilot_ignore_observer
68-
):
69-
copilot_ignore_observer.add_folders(window.folders())
70-
CopilotIgnore(window).load_patterns()
71-
CopilotIgnore(window).trigger(self.view)
72-
if get_copilot_view_setting(self.view, "is_copilot_ignored", False):
73-
plugin.update_status_bar_text({"is_copilot_ignored": "ignored"})
74-
else:
75-
plugin.update_status_bar_text()
76-
7763
def on_deactivated_async(self) -> None:
7864
ViewCompletionManager(self.view).hide()
7965

@@ -121,6 +107,16 @@ def test(value: Any) -> bool | None:
121107

122108
return None
123109

110+
def on_activated_async(self) -> None:
111+
_, session = CopilotPlugin.plugin_session(self.view)
112+
if (session and CopilotPlugin.should_ignore(self.view)) or (
113+
not session and not CopilotPlugin.should_ignore(self.view)
114+
):
115+
# Hacky way to trigger adding and removing views from session
116+
prev_setting = self.view.settings().get("lsp_uri")
117+
self.view.settings().set("lsp_uri", "")
118+
sublime.set_timeout_async(lambda: self.view.settings().set("lsp_uri", prev_setting), 5)
119+
124120
def on_post_text_command(self, command_name: str, args: dict[str, Any] | None) -> None:
125121
if command_name == "lsp_save":
126122
self._is_saving = True
@@ -133,7 +129,7 @@ def on_post_text_command(self, command_name: str, args: dict[str, Any] | None) -
133129
def on_post_save_async(self) -> None:
134130
self._is_saving = False
135131

136-
@_must_be_active_view_not_ignored()
132+
@_must_be_active_view()
137133
def on_selection_modified_async(self) -> None:
138134
if not self._is_modified:
139135
ViewCompletionManager(self.view).handle_selection_change()

plugin/utils.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
_T_Number = TypeVar("_T_Number", bound=Union[int, float])
2424

2525

26+
def all_windows() -> list[sublime.Window]:
27+
return sublime.windows()
28+
29+
2630
def all_views(
2731
window: sublime.Window | None = None,
2832
*,
@@ -302,9 +306,6 @@ def cleanup(cls):
302306
for view in all_views():
303307
erase_copilot_view_setting(view, "is_copilot_ignored")
304308

305-
def log_info(self, message: str):
306-
print(f"[COPILOT IGNORE] {message}\n")
307-
308309
def unload_patterns(self):
309310
self.patterns.clear()
310311
erase_copilot_setting(self.window, COPILOT_WINDOW_SETTINGS_PREFIX, "copilotignore.patterns")
@@ -349,9 +350,4 @@ def trigger(self, view: sublime.View):
349350
if not file:
350351
return False
351352

352-
if self.matches_any_pattern(file):
353-
set_copilot_view_setting(view, "is_copilot_ignored", True)
354-
return True
355-
else:
356-
erase_copilot_view_setting(view, "is_copilot_ignored")
357-
return False
353+
return self.matches_any_pattern(file)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
%YAML 1.2
2+
---
3+
name: Copilot Ignore
4+
scope: text.copilotignore
5+
version: 2
6+
hidden: true
7+
hidden_file_extensions:
8+
- .copilotignore
9+
10+
contexts:
11+
main:
12+
- include: Git Common.sublime-syntax#comments
13+
- match: '(?=\S)'
14+
push: [pattern-content, Git Common.sublime-syntax#fnmatch-start]
15+
16+
pattern-content:
17+
- meta_scope: string.unquoted.copilotignore entity.name.pattern.copilotignore
18+
- match: $
19+
pop: 1
20+
- include: Git Common.sublime-syntax#fnmatch-unquoted-body

0 commit comments

Comments
 (0)