-
-
Notifications
You must be signed in to change notification settings - Fork 105
Description
Describe the bug
When frontMatter.content.autoUpdateDate is enabled and editing a document that doesn't have a trailing newline, the automatic date updates are triggered twice. This doesn't happen when the file does have a trailing new line. For example, after it updates twice (the newline is added for you), subsequent updates happen only once.
To Reproduce
Steps to reproduce the behavior:
- Ensure
frontMatter.content.autoUpdateDateis enabled. - Try to update a markdown (or MDX) file and wait for the debounce to trigger the autoupdate.
- Observe that the lastmod field is updated twice.
Expected behavior
I'd expect the lastmod field to be updated only once.
Screenshots
A gif which demonstrates the behavior:

VS Code Version
Version: 1.63.2 (Universal)
Commit: 899d46d82c4c95423fb7e10e68eba52050e30ba3
Date: 2021-12-15T09:37:28.172Z (2 wks ago)
Electron: 13.5.2
Chromium: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Darwin arm64 21.2.0
Extension Version
v5.9.0
Additional context
This is likely due to the autoupdate logic defined here:
vscode-front-matter/src/commands/Article.ts
Lines 243 to 265 in 4d38a08
| public static async autoUpdate(fileChanges: vscode.TextDocumentChangeEvent) { | |
| const txtChanges = fileChanges.contentChanges.map(c => c.text); | |
| const editor = vscode.window.activeTextEditor; | |
| if (txtChanges.length > 0 && editor && ArticleHelper.isMarkdownFile()) { | |
| const autoUpdate = Settings.get(SETTING_AUTO_UPDATE_DATE); | |
| if (autoUpdate) { | |
| const article = ArticleHelper.getFrontMatter(editor); | |
| if (!article) { | |
| return; | |
| } | |
| if (article.content === Article.prevContent) { | |
| return; | |
| } | |
| Article.prevContent = article.content; | |
| Article.setLastModifiedDate(); | |
| } | |
| } | |
| } |
One way to solve this might be to adjust the change detection logic, but this may introduce new bugs.
Another possible solution would be to do away with change detection entirely-- moving away from vscode.workspace.onDidChangeTextDocument to onWillSaveTextDocument: https://code.visualstudio.com/api/references/vscode-api#3591 . This would move auto-updates to the pre-save lifecycle which would prevent the extension from updating the lastmod timestamp while the user is actively editing the document.