11import * as vscode from 'vscode' ;
22import { CodeOrganizerDocumentSymbolProvider } from './documentSymbolProvider' ;
3+ import { CodeOrganizerTreeDataProvider } from './treeDataProvider' ;
4+ import { initializeDecorations , updateSectionHighlight , disposeDecorations } from './decorations' ;
5+ import { SectionMatch } from './utils/findSections' ;
36
47export function activate ( context : vscode . ExtensionContext ) {
5-
8+
69 // Show activation message
710 vscode . window . showInformationMessage ( 'Code Organizer extension activated!' ) ;
8-
11+
912 // Get configuration
1013 const config = vscode . workspace . getConfiguration ( 'codeOrganizer' ) ;
1114 const isEnabled = config . get < boolean > ( 'enable' , true ) ;
@@ -16,7 +19,7 @@ export function activate(context: vscode.ExtensionContext) {
1619 return ;
1720 }
1821
19- // Register document symbol provider
22+ // Register document symbol provider (keep for built-in Outline)
2023 const provider = new CodeOrganizerDocumentSymbolProvider ( ) ;
2124
2225 if ( supportedLanguages . includes ( '*' ) ) {
@@ -39,6 +42,145 @@ export function activate(context: vscode.ExtensionContext) {
3942 } ) ;
4043 }
4144
45+ // Create custom TreeView for enhanced outline with highlighting
46+ const treeDataProvider = new CodeOrganizerTreeDataProvider ( ) ;
47+ const treeView = vscode . window . createTreeView ( 'codeOrganizerOutline' , {
48+ treeDataProvider : treeDataProvider ,
49+ showCollapseAll : true
50+ } ) ;
51+ context . subscriptions . push ( treeView ) ;
52+ console . log ( '[Code Organizer] TreeView created' ) ;
53+
54+ // Initialize editor decorations
55+ const decoration = initializeDecorations ( ) ;
56+ context . subscriptions . push ( decoration ) ;
57+
58+ // Register "Go to Section" command
59+ context . subscriptions . push (
60+ vscode . commands . registerCommand (
61+ 'codeOrganizer.goToSection' ,
62+ ( section : SectionMatch , document : vscode . TextDocument ) => {
63+ const editor = vscode . window . activeTextEditor ;
64+ if ( editor && editor . document === document ) {
65+ const position = document . positionAt ( section . index ) ;
66+ editor . selection = new vscode . Selection ( position , position ) ;
67+ editor . revealRange (
68+ new vscode . Range ( position , position ) ,
69+ vscode . TextEditorRevealType . InCenter
70+ ) ;
71+ }
72+ }
73+ )
74+ ) ;
75+
76+ // Helper function to find current section from cursor position
77+ function getCurrentSection (
78+ cursorPos : vscode . Position ,
79+ document : vscode . TextDocument ,
80+ sections : SectionMatch [ ]
81+ ) : SectionMatch | undefined {
82+ const offset = document . offsetAt ( cursorPos ) ;
83+ let deepestSection : SectionMatch | undefined ;
84+
85+ for ( const section of sections ) {
86+ const sectionStart = section . index ;
87+ // Find next section at same or higher level to determine end
88+ const nextSection = sections . find (
89+ s => s . index > section . index && s . depth <= section . depth
90+ ) ;
91+ const sectionEnd = nextSection ? nextSection . index : document . getText ( ) . length ;
92+
93+ if ( offset >= sectionStart && offset < sectionEnd ) {
94+ // Found a containing section, keep the deepest one
95+ if ( ! deepestSection || section . depth > deepestSection . depth ) {
96+ deepestSection = section ;
97+ }
98+ }
99+ }
100+
101+ return deepestSection ;
102+ }
103+
104+ // Update highlight function with caching
105+ let updateTimeout : NodeJS . Timeout | undefined ;
106+ let lastDocument : vscode . TextDocument | undefined ;
107+
108+ async function updateHighlight ( ) {
109+ const editor = vscode . window . activeTextEditor ;
110+ if ( ! editor ) {
111+ console . log ( '[Code Organizer] No active editor' ) ;
112+ return ;
113+ }
114+
115+ const document = editor . document ;
116+
117+ // Refresh tree if document changed
118+ if ( document !== lastDocument ) {
119+ console . log ( '[Code Organizer] Refreshing tree for document:' , document . fileName ) ;
120+ treeDataProvider . refresh ( document ) ;
121+ lastDocument = document ;
122+ }
123+
124+ const cursorPos = editor . selection . active ;
125+ const sections = treeDataProvider . getSections ( ) ;
126+ console . log ( '[Code Organizer] Found' , sections . length , 'sections' ) ;
127+ const currentSection = getCurrentSection ( cursorPos , document , sections ) ;
128+ console . log ( '[Code Organizer] Current section:' , currentSection ?. name ) ;
129+
130+ // Update editor decoration
131+ updateSectionHighlight ( currentSection , editor , decoration ) ;
132+
133+ // Update tree view highlight
134+ if ( currentSection ) {
135+ // Get the cached tree item instance for reveal to work
136+ const item = treeDataProvider . findTreeItemBySection ( currentSection ) ;
137+ console . log ( '[Code Organizer] Tree item found:' , ! ! item ) ;
138+ if ( item ) {
139+ try {
140+ await treeView . reveal ( item , {
141+ select : true ,
142+ focus : false ,
143+ expand : 1
144+ } ) ;
145+ console . log ( '[Code Organizer] Reveal succeeded' ) ;
146+ } catch ( error ) {
147+ console . error ( '[Code Organizer] Reveal failed:' , error ) ;
148+ }
149+ }
150+ }
151+ }
152+
153+ // Listen for cursor changes (with debouncing)
154+ context . subscriptions . push (
155+ vscode . window . onDidChangeTextEditorSelection ( ( ) => {
156+ if ( updateTimeout ) {
157+ clearTimeout ( updateTimeout ) ;
158+ }
159+ updateTimeout = setTimeout ( updateHighlight , 150 ) ;
160+ } )
161+ ) ;
162+
163+ // Listen for editor switches
164+ context . subscriptions . push (
165+ vscode . window . onDidChangeActiveTextEditor ( ( ) => {
166+ updateHighlight ( ) ;
167+ } )
168+ ) ;
169+
170+ // Clear cache on document changes
171+ context . subscriptions . push (
172+ vscode . workspace . onDidChangeTextDocument ( ( event ) => {
173+ if ( event . document === lastDocument ) {
174+ lastDocument = undefined ;
175+ }
176+ } )
177+ ) ;
178+
179+ // Initial highlight
180+ if ( vscode . window . activeTextEditor ) {
181+ updateHighlight ( ) ;
182+ }
183+
42184 // Register activation command as fallback
43185 const activateCommand = vscode . commands . registerCommand ( 'codeOrganizer.activate' , ( ) => {
44186 vscode . window . showInformationMessage ( 'Code Organizer is already active and working!' ) ;
@@ -63,5 +205,6 @@ export function activate(context: vscode.ExtensionContext) {
63205}
64206
65207export function deactivate ( ) {
66- // Cleanup if needed
208+ // Cleanup decorations
209+ disposeDecorations ( ) ;
67210}
0 commit comments