Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/WebviewManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { WebviewMessage } from './types';
import { SearchState, WebviewMessage } from './types';
import { SearchService } from './services/SearchService';
import { FileService } from './services/FileService';
import { SyntaxHighlightService } from './services/SyntaxHighlightService';
Expand All @@ -16,6 +16,7 @@ export class WebviewManager {
private syntaxHighlightService: SyntaxHighlightService;
private disposables: vscode.Disposable[] = [];
private panelCounter: number = 0;
private lastSearchState: SearchState | undefined;

constructor(private context: vscode.ExtensionContext) {
this.searchService = new SearchService();
Expand All @@ -37,7 +38,7 @@ export class WebviewManager {
return;
}

this.createPanel();
this.createPanel(true);
}

showNewTab(initialSearchText?: string): void {
Expand Down Expand Up @@ -80,7 +81,7 @@ export class WebviewManager {
this.syntaxHighlightService.dispose();
}

private createPanel(): string {
private createPanel(restoreLastState: boolean = false): string {
this.panelCounter++;
const panelId = `search-${this.panelCounter}`;
const tabNumber = this.panels.size + 1;
Expand Down Expand Up @@ -129,6 +130,16 @@ export class WebviewManager {
});
this.setupMessageHandler(panelId, panel);
this.setupPanelDisposal(panelId, panel);

if (restoreLastState && this.lastSearchState) {
setTimeout(() => {
panel.webview.postMessage({
command: 'restoreSearchState',
state: this.lastSearchState
});
}, 100);
}
Comment on lines +134 to +141

return panelId;
}

Expand Down Expand Up @@ -156,6 +167,10 @@ export class WebviewManager {
}
break;

case 'updateSearchState':
this.lastSearchState = message.state;
break;

case 'getFileContent':
if (message.filePath) {
await this.handleGetFileContent(panel, message.filePath);
Expand Down
8 changes: 6 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ export function activate(context: vscode.ExtensionContext): void {
}
}

// Fallback: open normal search tab (potentially with selected text)
webviewManager?.showNewTab(selectedText);
// Fallback: selected text starts a fresh search; otherwise return to the last search.
if (selectedText) {
webviewManager?.showNewTab(selectedText);
} else {
webviewManager?.show();
}
Comment on lines +47 to +52
}
);

Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ export interface FileSearchResult {
icon?: ResolvedIconDefinition;
}

export interface SearchState {
text: string;
fileMask: string;
scope: 'project' | 'directory';
scopePath: string;
}

export type WebviewMessage = {
command: 'search'
text: string;
includePattern?: string;
excludePattern?: string;
} | {
command: 'updateSearchState'
state: SearchState;
} | {
command: 'getFileContent'
filePath: string;
Expand Down
84 changes: 74 additions & 10 deletions src/webview/script.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference lib="dom" />

import { FileSearchResult, SearchMatch, WebviewMessage } from "../types";
import { FileSearchResult, SearchMatch, SearchState, WebviewMessage } from "../types";


type SearchMatchWithId = SearchMatch & { matchId: number, icon?: FileSearchResult['icon'] };
Expand Down Expand Up @@ -42,6 +42,7 @@ type SearchMatchWithId = SearchMatch & { matchId: number, icon?: FileSearchResul
}
} = {};
let searchTimeout: any = null;
let persistSearchStateTimeout: any = null;

window.addEventListener('message', (event) => {
const message = event.data;
Expand Down Expand Up @@ -70,9 +71,44 @@ type SearchMatchWithId = SearchMatch & { matchId: number, icon?: FileSearchResul
case 'setInitialSearchText':
handleSetInitialSearchText(message.text);
break;
case 'restoreSearchState':
handleRestoreSearchState(message.state);
break;
}
});

function getSearchState(): SearchState {
return {
text: (searchInput as HTMLInputElement).value.trim(),
fileMask,
scope: currentScope === 'directory' ? 'directory' : 'project',
scopePath
};
}

function persistSearchState() {
if (persistSearchStateTimeout) {
clearTimeout(persistSearchStateTimeout);
persistSearchStateTimeout = null;
}

postMessage({
command: 'updateSearchState',
state: getSearchState()
});
}

function schedulePersistSearchState() {
if (persistSearchStateTimeout) {
clearTimeout(persistSearchStateTimeout);
}

persistSearchStateTimeout = setTimeout(() => {
persistSearchStateTimeout = null;
persistSearchState();
}, 250);
}

/**
* Converts directory paths to glob patterns for file filtering.
* Supports comma-separated paths and preserves existing glob patterns.
Expand Down Expand Up @@ -105,6 +141,7 @@ type SearchMatchWithId = SearchMatch & { matchId: number, icon?: FileSearchResul

function performSearch() {
const searchText = (searchInput as HTMLInputElement).value.trim();
schedulePersistSearchState();

if (!searchText) {
clearResults();
Expand Down Expand Up @@ -174,6 +211,7 @@ type SearchMatchWithId = SearchMatch & { matchId: number, icon?: FileSearchResul
filterToggleButton.classList.add('active');
fileMaskInput.focus();
}
persistSearchState();
});

fileMaskInput.addEventListener('input', () => {
Expand Down Expand Up @@ -476,18 +514,27 @@ type SearchMatchWithId = SearchMatch & { matchId: number, icon?: FileSearchResul
performSearch();
}

function handleSetInitialDirectory(path: string, searchText?: string) {
// Switch to directory scope
scopeButtons.forEach(btn => btn.classList.remove('active'));
const directoryButton = document.querySelector('[data-scope="directory"]');
if (directoryButton) {
directoryButton.classList.add('active');
function applyScope(scope: string) {
scopeButtons.forEach(btn => {
btn.classList.toggle('active', btn.getAttribute('data-scope') === scope);
});

currentScope = scope === 'directory' ? 'directory' : 'project';

if (currentScope === 'directory') {
scopeInputContainer.style.display = 'flex';
} else {
scopeInputContainer.style.display = 'none';
scopePath = '';
scopePathInput.value = '';
Comment on lines +518 to +529
}
}

currentScope = 'directory';
function handleSetInitialDirectory(path: string, searchText?: string) {
// Switch to directory scope
applyScope('directory');
scopePath = path;
scopePathInput.value = path;
scopeInputContainer.style.display = 'flex';

// Set initial search text if provided
if (searchText) {
Expand All @@ -501,6 +548,23 @@ type SearchMatchWithId = SearchMatch & { matchId: number, icon?: FileSearchResul
performSearch();
}

function handleRestoreSearchState(state: SearchState) {
(searchInput as HTMLInputElement).value = state.text;

fileMask = state.fileMask;
fileMaskInput.value = state.fileMask;
filterContainer.style.display = state.fileMask ? 'flex' : 'none';
filterToggleButton.classList.toggle('active', Boolean(state.fileMask));

applyScope(state.scope);
if (state.scope === 'directory') {
scopePath = state.scopePath;
scopePathInput.value = state.scopePath;
}

performSearch();
}

function displayFilePreview(filePath: string, lineNumber: number, columnNumber: number) {
const cached = fileContentsCache[filePath];
if (!cached) return;
Expand Down Expand Up @@ -701,4 +765,4 @@ type SearchMatchWithId = SearchMatch & { matchId: number, icon?: FileSearchResul
});

searchInput.focus();
}());
}());