Skip to content

Commit def429b

Browse files
CopilotChristopher-C-Robinson
authored andcommitted
Refactor: Extract duplicate pattern matching logic and fix event.waitUntil() timing
Co-authored-by: Christopher-C-Robinson <78235938+Christopher-C-Robinson@users.noreply.github.com>
1 parent 99bcdf9 commit def429b

1 file changed

Lines changed: 68 additions & 71 deletions

File tree

src/extension.ts

Lines changed: 68 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { fileExists, getDirectoryPath, getFullPath, getRelativePath, joinPath, r
88
import { log, setLogger } from "./tsco-cli/source-code/source-code-logger";
99
import { SourceCodeOrganizer } from "./tsco-cli/source-code/source-code-organizer";
1010

11-
// #region Functions (10)
11+
// #region Functions (11)
1212

1313
async function getConfiguration(configurationFilePath: string | null)
1414
{
@@ -79,6 +79,34 @@ function matches(pattern: string, text: string)
7979
return globToRegExp(pattern).test(text);
8080
}
8181

82+
function shouldOrganizeFile(sourceCodeFilePathRelative: string, configuration: Configuration): { shouldOrganize: boolean, reason?: string }
83+
{
84+
let include = true;
85+
let exclude = false;
86+
87+
if (configuration.files.include.length > 0)
88+
{
89+
include = configuration.files.include.some(inc => matches(inc, sourceCodeFilePathRelative) || matches(inc, sourceCodeFilePathRelative.replaceAll("../", "").replaceAll("./", "")));
90+
}
91+
92+
if (configuration.files.exclude.length > 0)
93+
{
94+
exclude = configuration.files.exclude.some(exc => matches(exc, sourceCodeFilePathRelative) || matches(exc, sourceCodeFilePathRelative.replaceAll("../", "").replaceAll("./", "")));
95+
}
96+
97+
if (!include)
98+
{
99+
return { shouldOrganize: false, reason: "does not match file include patterns" };
100+
}
101+
102+
if (exclude)
103+
{
104+
return { shouldOrganize: false, reason: "matches file exclude patterns" };
105+
}
106+
107+
return { shouldOrganize: true };
108+
}
109+
82110
async function onInitialize()
83111
{
84112
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0)
@@ -152,27 +180,20 @@ async function onSave(event: vscode.TextDocumentWillSaveEvent)
152180

153181
if (matches("**/*.ts", sourceCodeFilePath))
154182
{
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)
165-
{
166-
include = configuration.files.include.some(inc => matches(inc, sourceCodeFilePathRelative) || matches(inc, sourceCodeFilePathRelative.replaceAll("../", "").replaceAll("./", "")));
167-
}
168-
169-
if (configuration.files.exclude.length > 0)
170-
{
171-
exclude = configuration.files.exclude.some(exc => matches(exc, sourceCodeFilePathRelative) || matches(exc, sourceCodeFilePathRelative.replaceAll("../", "").replaceAll("./", "")));
172-
}
173-
174-
if (include && !exclude)
175-
{
183+
event.waitUntil((async () => {
184+
const configuration = await getConfiguration(settings.configurationFilePath);
185+
const workspaceRootDirectoryPath = getWorkspaceRootDirectoryPath();
186+
const sourceCodeDirectoryPath = workspaceRootDirectoryPath;
187+
const sourceCodeFilePathRelative = getRelativePath(sourceCodeDirectoryPath, sourceCodeFilePath);
188+
189+
const fileCheck = shouldOrganizeFile(sourceCodeFilePathRelative, configuration);
190+
191+
if (!fileCheck.shouldOrganize)
192+
{
193+
log(`tsco skipping organizing ${sourceCodeFilePath}, because it ${fileCheck.reason}`);
194+
return [];
195+
}
196+
176197
const sourceCode = event.document.getText();
177198
const organizedSourceCode = await SourceCodeOrganizer.organizeSourceCode(sourceCodeFilePath, sourceCode, configuration);
178199

@@ -182,23 +203,16 @@ async function onSave(event: vscode.TextDocumentWillSaveEvent)
182203
const end = new vscode.Position(event.document.lineCount - 1, event.document.lineAt(event.document.lineCount - 1).text.length);
183204
const range = new vscode.Range(start, end);
184205

185-
event.waitUntil(Promise.resolve([vscode.TextEdit.replace(range, organizedSourceCode)]));
186-
187206
log(`tsco organized ${sourceCodeFilePath}`);
207+
208+
return [vscode.TextEdit.replace(range, organizedSourceCode)];
188209
}
189210
else
190211
{
191212
log(`tsco skipping organizing ${sourceCodeFilePath}, because it is already organized`);
213+
return [];
192214
}
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`);
201-
}
215+
})());
202216
}
203217
}
204218
}
@@ -223,55 +237,38 @@ async function organize(sourceCodeFilePath: string, configuration: Configuration
223237
const sourceCodeDirectoryPath = workspaceRootDirectoryPath;
224238
const sourceCodeFilePathRelative = getRelativePath(sourceCodeDirectoryPath, sourceCodeFilePath);
225239

226-
// test for include or exclude patterns
227-
let include = true;
228-
let exclude = false;
229-
230-
if (configuration.files.include.length > 0)
240+
const fileCheck = shouldOrganizeFile(sourceCodeFilePathRelative, configuration);
241+
242+
if (!fileCheck.shouldOrganize)
231243
{
232-
include = configuration.files.include.some(inc => matches(inc, sourceCodeFilePathRelative) || matches(inc, sourceCodeFilePathRelative.replaceAll("../", "").replaceAll("./", "")));
244+
log(`tsco skipping organizing ${sourceCodeFilePath}, because it ${fileCheck.reason}`);
245+
return false;
233246
}
234247

235-
if (configuration.files.exclude.length > 0)
236-
{
237-
exclude = configuration.files.exclude.some(exc => matches(exc, sourceCodeFilePathRelative) || matches(exc, sourceCodeFilePathRelative.replaceAll("../", "").replaceAll("./", "")));
238-
}
248+
// organize and save
249+
let editor = await getOpenedEditor(sourceCodeFilePath);
250+
const sourceCode = editor ? editor.document.getText() : await readFile(sourceCodeFilePath);
251+
const organizedSourceCode = await SourceCodeOrganizer.organizeSourceCode(sourceCodeFilePath, sourceCode, configuration);
239252

240-
if (include && !exclude)
253+
if (organizedSourceCode !== sourceCode)
241254
{
242-
// organize and save
243-
let editor = await getOpenedEditor(sourceCodeFilePath);
244-
const sourceCode = editor ? editor.document.getText() : await readFile(sourceCodeFilePath);
245-
const organizedSourceCode = await SourceCodeOrganizer.organizeSourceCode(sourceCodeFilePath, sourceCode, configuration);
246-
247-
if (organizedSourceCode !== sourceCode)
248-
{
249-
editor ??= await openEditor(sourceCodeFilePath);
250-
const start = new vscode.Position(0, 0);
251-
const end = new vscode.Position(editor.document.lineCount, editor.document.lineAt(editor.document.lineCount - 1).text.length);
252-
const range = new vscode.Range(start, end);
253-
const edit = new vscode.WorkspaceEdit();
255+
editor ??= await openEditor(sourceCodeFilePath);
256+
const start = new vscode.Position(0, 0);
257+
const end = new vscode.Position(editor.document.lineCount - 1, editor.document.lineAt(editor.document.lineCount - 1).text.length);
258+
const range = new vscode.Range(start, end);
259+
const edit = new vscode.WorkspaceEdit();
254260

255-
edit.replace(editor.document.uri, range, organizedSourceCode);
261+
edit.replace(editor.document.uri, range, organizedSourceCode);
256262

257-
await vscode.workspace.applyEdit(edit);
263+
await vscode.workspace.applyEdit(edit);
258264

259-
log(`tsco organized ${sourceCodeFilePath}`);
265+
log(`tsco organized ${sourceCodeFilePath}`);
260266

261-
return true;
262-
}
263-
else
264-
{
265-
log(`tsco skipping organizing ${sourceCodeFilePath}, because it is already organized`);
266-
}
267+
return true;
267268
}
268-
else if (!include)
269-
{
270-
log(`tsco skipping organizing ${sourceCodeFilePath}, because it does not match file include patterns`);
271-
}
272-
else if (exclude)
269+
else
273270
{
274-
log(`tsco skipping organizing ${sourceCodeFilePath}, because it matches file exclude patterns`);
271+
log(`tsco skipping organizing ${sourceCodeFilePath}, because it is already organized`);
275272
}
276273

277274
return false;

0 commit comments

Comments
 (0)