@@ -34,7 +34,10 @@ import {
3434} from "@emmetio/codemirror6-plugin" ;
3535import createBaseExtensions from "cm/baseExtensions" ;
3636import lspClientManager from "cm/lsp/clientManager" ;
37- import lspDiagnosticsExtension from "cm/lsp/diagnostics" ;
37+ import lspDiagnosticsExtension , {
38+ getLspDiagnostics ,
39+ LSP_DIAGNOSTICS_EVENT ,
40+ } from "cm/lsp/diagnostics" ;
3841import { stopManagedServer } from "cm/lsp/serverLauncher" ;
3942import serverRegistry from "cm/lsp/serverRegistry" ;
4043// CodeMirror mode management
@@ -1103,6 +1106,29 @@ async function EditorManager($header, $body) {
11031106 } ,
11041107 } ;
11051108
1109+ if ( typeof document !== "undefined" ) {
1110+ const globalTarget =
1111+ typeof globalThis !== "undefined" ? globalThis : document ;
1112+ const diagnosticsListenerKey = "__acodeDiagnosticsListener" ;
1113+ const existing = globalTarget ?. [ diagnosticsListenerKey ] ;
1114+ if ( typeof existing === "function" ) {
1115+ document . removeEventListener ( LSP_DIAGNOSTICS_EVENT , existing ) ;
1116+ }
1117+ const listener = ( ) => {
1118+ const active = manager . activeFile ;
1119+ if ( active ?. type === "editor" ) {
1120+ try {
1121+ active . session = editor . state ;
1122+ } catch ( _ ) { }
1123+ }
1124+ toggleProblemButton ( ) ;
1125+ } ;
1126+ document . addEventListener ( LSP_DIAGNOSTICS_EVENT , listener ) ;
1127+ if ( globalTarget ) {
1128+ globalTarget [ diagnosticsListenerKey ] = listener ;
1129+ }
1130+ }
1131+
11061132 lspClientManager . setOptions ( {
11071133 resolveRoot : resolveRootUriForContext ,
11081134 onClientIdle : ( { server } ) => {
@@ -1268,6 +1294,7 @@ async function EditorManager($header, $body) {
12681294 appSettings . on ( "update:showSideButtons" , function ( ) {
12691295 updateMargin ( ) ;
12701296 updateSideButtonContainer ( ) ;
1297+ toggleProblemButton ( ) ;
12711298 } ) ;
12721299
12731300 appSettings . on ( "update:showAnnotations" , function ( ) {
@@ -1311,6 +1338,7 @@ async function EditorManager($header, $body) {
13111338 events . emit ( "file-content-changed" , file ) ;
13121339 manager . onupdate ( "file-changed" ) ;
13131340 manager . emit ( "update" , "file-changed" ) ;
1341+ toggleProblemButton ( ) ;
13141342
13151343 const { autosave } = appSettings . value ;
13161344 if ( file . uri && changed && autosave ) {
@@ -1348,6 +1376,7 @@ async function EditorManager($header, $body) {
13481376
13491377 manager . on ( [ "remove-file" ] , ( file ) => {
13501378 detachLspForFile ( file ) ;
1379+ toggleProblemButton ( ) ;
13511380 } ) ;
13521381
13531382 manager . on ( [ "rename-file" ] , ( file ) => {
@@ -1375,6 +1404,7 @@ async function EditorManager($header, $body) {
13751404 manager . files . push ( file ) ;
13761405 manager . openFileList . append ( file . tab ) ;
13771406 $header . text = file . name ;
1407+ toggleProblemButton ( ) ;
13781408 }
13791409
13801410 /**
@@ -1512,6 +1542,7 @@ async function EditorManager($header, $body) {
15121542
15131543 updateMargin ( true ) ;
15141544 updateSideButtonContainer ( ) ;
1545+ toggleProblemButton ( ) ;
15151546 // TODO: Implement scroll margin for CodeMirror
15161547 // editor.renderer.setScrollMargin(
15171548 // scrollMarginTop,
@@ -1738,18 +1769,48 @@ async function EditorManager($header, $body) {
17381769 /**
17391770 * Toggles the visibility of the problem button based on the presence of annotations in the files.
17401771 */
1741- // TODO: Implement problem button toggle for CodeMirror
1772+ function fileHasProblems ( file ) {
1773+ const state = getDiagnosticStateForFile ( file ) ;
1774+ if ( ! state ) return false ;
1775+
1776+ const session = file . session ;
1777+ if ( session && typeof session . getAnnotations === "function" ) {
1778+ try {
1779+ const annotations = session . getAnnotations ( ) || [ ] ;
1780+ if ( annotations . length ) return true ;
1781+ } catch ( _ ) { }
1782+ }
1783+
1784+ if ( typeof state . field !== "function" ) return false ;
1785+ try {
1786+ const diagnostics = getLspDiagnostics ( state ) ;
1787+ return diagnostics . length > 0 ;
1788+ } catch ( _ ) { }
1789+
1790+ return false ;
1791+ }
1792+
17421793 function toggleProblemButton ( ) {
1743- // const fileWithProblems = manager.files.find((file) => {
1744- // if (file.type !== "editor") return false;
1745- // const annotations = file?.session?.getAnnotations();
1746- // return !!annotations.length;
1747- // });
1748- // if (fileWithProblems) {
1749- // problemButton.show();
1750- // } else {
1751- // problemButton.hide();
1752- // }
1794+ const { showSideButtons } = appSettings . value ;
1795+ if ( ! showSideButtons ) {
1796+ problemButton . hide ( ) ;
1797+ return ;
1798+ }
1799+
1800+ const hasProblems = manager . files . some ( ( file ) => fileHasProblems ( file ) ) ;
1801+ if ( hasProblems ) {
1802+ problemButton . show ( ) ;
1803+ } else {
1804+ problemButton . hide ( ) ;
1805+ }
1806+ }
1807+
1808+ function getDiagnosticStateForFile ( file ) {
1809+ if ( ! file || file . type !== "editor" ) return null ;
1810+ if ( manager . activeFile ?. id === file . id && editor ?. state ) {
1811+ return editor . state ;
1812+ }
1813+ return file . session || null ;
17531814 }
17541815
17551816 /**
@@ -1863,6 +1924,8 @@ async function EditorManager($header, $body) {
18631924 $header . text = file . filename ;
18641925 manager . onupdate ( "switch-file" ) ;
18651926 events . emit ( "switch-file" , file ) ;
1927+
1928+ toggleProblemButton ( ) ;
18661929 }
18671930
18681931 /**
0 commit comments