Skip to content

Commit

Permalink
offer to clean up vspace if saved with
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Jul 22, 2021
1 parent 64335c3 commit 6289bc8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the "vscode-fake-virtual-space" extension will be documented in this file.

## 0.1.3

- If saved file with fake vspace, offer to clean up and save again

## 0.1.2

- Implement fake vspace on vertical movement with word wrap
Expand Down
37 changes: 26 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,16 @@ export function activate(context: vscode.ExtensionContext) {
if (!lock) lock=new Mutex() // see for reasons: https://github.com/jemc/vscode-implicit-indent
if (!undoLock) undoLock=new Mutex()
vscode.workspace.onDidChangeTextDocument(onDidChangeTextDocumentListener)
/*
vscode.workspace.onWillSaveTextDocument(event=>{
// could just run undo if was sure that document text is in focus, but there's no way to check it
// return vscode.commands.executeCommand('undo')
// yes, you can save from find popup, undos land on popup text input in this case
const state=getDocumentState(event.document)
if (!state.vspace) return
vscode.window.showInformationMessage('Saved with fake virtual space - unless there are other onsave handlers that clean it up. Consider saving from outside of virtual space.')
// this is the cleanup code which damages redo stack - we'd rather not do anything:
// const lineRange=event.document.lineAt(state.vspace).range
// const edit=vscode.TextEdit.delete(new vscode.Range(state.vspace,lineRange.end))
// event.waitUntil((async()=>[edit])())
// otherwise could do the following a cleanup edit, but it damages redo stack - we'd rather not do anything:
const lineRange=event.document.lineAt(state.vspace).range
const edit=vscode.TextEdit.delete(new vscode.Range(state.vspace,lineRange.end))
event.waitUntil((async()=>[edit])())
})
*/
vscode.workspace.onDidSaveTextDocument(onDidSaveTextDocumentListener)
vscode.workspace.onDidCloseTextDocument(document=>{
documentStates.delete(document)
})
Expand All @@ -66,12 +64,11 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('fakeVirtualSpace.cursorEnd',cursorEnd),
vscode.commands.registerCommand('fakeVirtualSpace.undo',undo),
vscode.commands.registerCommand('fakeVirtualSpace.redo',redo)
// TODO have find handler that cleans up vspace first
)
}

export function deactivate() {
// TODO do undos if necessary
// TODO do undos if necessary - or warn
}

async function onDidChangeTextEditorSelectionListener(event:vscode.TextEditorSelectionChangeEvent) {
Expand Down Expand Up @@ -110,6 +107,24 @@ async function onDidChangeTextEditorSelectionListener(event:vscode.TextEditorSel
}
}

async function onDidSaveTextDocumentListener(document:vscode.TextDocument) {
const state=getDocumentState(document)
if (!state.vspace) return
vscode.window.showInformationMessage(
`Saved ${document.fileName} with fake virtual space`,
'Clean up and save again'
).then(async(value)=>{
if (value==null) return
if (document.isClosed) {
vscode.window.showErrorMessage(`File ${document.fileName} can't be saved again because it was closed`)
return
}
const editor=await vscode.window.showTextDocument(document)
await cleanupVspace(editor)
await document.save()
})
}

async function cursorEnd() {
const releaseLock=await lock.acquire()
try {
Expand Down

0 comments on commit 6289bc8

Please sign in to comment.