22// SPDX-License-Identifier: Apache-2.0
33
44package software .aws .toolkits .eclipse .amazonq .util ;
5-
65import static software .aws .toolkits .eclipse .amazonq .util .QEclipseEditorUtils .getActiveTextEditor ;
6+ import static software .aws .toolkits .eclipse .amazonq .util .QEclipseEditorUtils .getActiveTextViewer ;
77
88import org .eclipse .jface .text .IDocument ;
99import org .eclipse .jface .text .IDocumentListener ;
@@ -25,16 +25,43 @@ public AutoTriggerPartListener(final T docListener) {
2525 @ Override
2626 public void partActivated (final IWorkbenchPartReference partRef ) {
2727 var part = partRef .getPart (false );
28- if (!(part instanceof ITextEditor )) {
28+ boolean isEditor = part instanceof ITextEditor ;
29+ if (isEditor ) {
30+ ITextEditor editor = (ITextEditor ) part ;
31+
32+ // We should only have at most one listener listening to one document
33+ // at any given moment. Therefore it would be acceptable to override the
34+ // listener
35+ // This is also assuming an active part cannot be activated again.
36+ attachDocumentListenerAndUpdateActiveDocument (editor );
2937 return ;
3038 }
31- ITextEditor editor = (ITextEditor ) part ;
3239
33- // We should only have at most one listener listening to one document
34- // at any given moment. Therefore it would be acceptable to override the
35- // listener
36- // This is also assuming an active part cannot be activated again.
37- attachDocumentListenerAndUpdateActiveDocument (editor );
40+ /**
41+ * When users use ADT plugin and connect to SAP server, the file opened is a customized editor defined by SAP package.
42+ * com.sap.adt.programs.ui.internal.programs.editors.ProgramEditor, which cause above logic not executed, inline listener is not attached hence inline auto trigger is broken.
43+ * Here is a monkey patch to fix the issue based on how we currently instrument inline auto trigger.
44+ * We might need to add different class names if we see more variants of such editor/file showing.
45+ */
46+ boolean isProgramEditor = part .getClass ().getName ().contains ("ProgramEditor" );
47+ if (isEditor ) {
48+ ITextEditor editor = (ITextEditor ) part ;
49+
50+ // We should only have at most one listener listening to one document
51+ // at any given moment. Therefore it would be acceptable to override the
52+ // listener
53+ // This is also assuming an active part cannot be activated again.
54+ attachDocumentListenerAndUpdateActiveDocument (editor );
55+ } else if (isProgramEditor ) {
56+ ITextEditor e = getActiveTextEditor ();
57+ var viewer = getActiveTextViewer (e );
58+ if (viewer != null ) {
59+ var document = viewer .getDocument ();
60+ if (document != null ) {
61+ attachDocumentListenerAndUpdateActiveDocument (document );
62+ }
63+ }
64+ }
3865 }
3966
4067 @ Override
@@ -48,8 +75,12 @@ public void partDeactivated(final IWorkbenchPartReference partRef) {
4875
4976 private void attachDocumentListenerAndUpdateActiveDocument (final ITextEditor editor ) {
5077 var document = editor .getDocumentProvider ().getDocument (editor .getEditorInput ());
78+ attachDocumentListenerAndUpdateActiveDocument (document );
79+ }
80+
81+ private void attachDocumentListenerAndUpdateActiveDocument (final IDocument document ) {
5182 document .addDocumentListener (docListener );
52- activeDocument = document ;
83+ setActiveDocument ( document ) ;
5384 }
5485
5586 private void detachDocumentListenerFromLastActiveDocument () {
0 commit comments