Skip to content

Commit 143beb3

Browse files
CopilotChristopher Robinson
authored andcommitted
Fix auto-save on focus change by using event.waitUntil() API
1 parent 8a51c80 commit 143beb3

1 file changed

Lines changed: 50 additions & 12 deletions

File tree

src/extension.ts

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,60 @@ async function onOrganizeAll()
144144
}
145145
}
146146

147-
async function onSave(sourceCodeFilePath: string)
147+
async function onSave(event: vscode.TextDocumentWillSaveEvent)
148148
{
149-
if (settings.organizeOnSave)
149+
if (settings.organizeOnSave && event.document.languageId === "typescript")
150150
{
151-
const editor = vscode.window.visibleTextEditors.find(ed => ed.document.uri.fsPath === sourceCodeFilePath);
152-
153-
if (editor)
151+
const sourceCodeFilePath = getFullPath(event.document.uri.fsPath);
152+
153+
if (matches("**/*.ts", sourceCodeFilePath))
154154
{
155-
if (await onOrganize(sourceCodeFilePath))
155+
const configuration = await getConfiguration(settings.configurationFilePath);
156+
const workspaceRootDirectoryPath = getWorkspaceRootDirectoryPath();
157+
const sourceCodeDirectoryPath = workspaceRootDirectoryPath;
158+
const sourceCodeFilePathRelative = getRelativePath(sourceCodeDirectoryPath, sourceCodeFilePath);
159+
160+
// test for include or exclude patterns
161+
let include = true;
162+
let exclude = false;
163+
164+
if (configuration.files.include.length > 0)
156165
{
157-
savingHandler.dispose();
166+
include = configuration.files.include.some(inc => matches(inc, sourceCodeFilePathRelative) || matches(inc, sourceCodeFilePathRelative.replace("../", "").replace("./", "")));
167+
}
158168

159-
await editor.document.save();
169+
if (configuration.files.exclude.length > 0)
170+
{
171+
exclude = configuration.files.exclude.some(exc => matches(exc, sourceCodeFilePathRelative) || matches(exc, sourceCodeFilePathRelative.replace("../", "").replace("./", "")));
172+
}
160173

161-
savingHandler = vscode.workspace.onWillSaveTextDocument(async (e) => await onSave(e.document.uri.fsPath));
174+
if (include && !exclude)
175+
{
176+
const sourceCode = event.document.getText();
177+
const organizedSourceCode = await SourceCodeOrganizer.organizeSourceCode(sourceCodeFilePath, sourceCode, configuration);
178+
179+
if (organizedSourceCode !== sourceCode)
180+
{
181+
const start = new vscode.Position(0, 0);
182+
const end = new vscode.Position(event.document.lineCount, event.document.lineAt(event.document.lineCount - 1).text.length);
183+
const range = new vscode.Range(start, end);
184+
185+
event.waitUntil(Promise.resolve([vscode.TextEdit.replace(range, organizedSourceCode)]));
186+
187+
log(`tsco organized ${sourceCodeFilePath}`);
188+
}
189+
else
190+
{
191+
log(`tsco skipping organizing ${sourceCodeFilePath}, because it is already organized`);
192+
}
193+
}
194+
else if (!include)
195+
{
196+
log(`tsco skipping organizing ${sourceCodeFilePath}, because it does not match file include patterns`);
197+
}
198+
else if (exclude)
199+
{
200+
log(`tsco skipping organizing ${sourceCodeFilePath}, because it matches file exclude patterns`);
162201
}
163202
}
164203
}
@@ -249,7 +288,7 @@ export function activate(context: vscode.ExtensionContext)
249288
context.subscriptions.push(vscode.commands.registerCommand('tsco.organizeAll', async () => await onOrganizeAll()));
250289

251290
vscode.workspace.onDidChangeConfiguration(() => settings = Settings.getSettings());
252-
savingHandler = vscode.workspace.onWillSaveTextDocument(async (e) => await onSave(e.document.uri.fsPath));
291+
context.subscriptions.push(vscode.workspace.onWillSaveTextDocument((e) => onSave(e)));
253292

254293
setLogger({
255294
log: (message: string) => output.appendLine(message),
@@ -259,11 +298,10 @@ export function activate(context: vscode.ExtensionContext)
259298

260299
// #endregion Exported Functions
261300

262-
// #region Variables (3)
301+
// #region Variables (2)
263302

264303
const output = vscode.window.createOutputChannel("tsco");
265304

266-
let savingHandler: vscode.Disposable;
267305
let settings = Settings.getSettings();
268306

269307
// #endregion Variables

0 commit comments

Comments
 (0)