Skip to content

Commit

Permalink
fix horizontal movement in vspace after save
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Jul 22, 2021
1 parent 6289bc8 commit 61a7a7d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to the "vscode-fake-virtual-space" extension will be documen
## 0.1.3

- If saved file with fake vspace, offer to clean up and save again
- Fix horizontal movement in vspace after save

## 0.1.2

Expand Down
21 changes: 17 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ let undoLock: Mutex;
class DocumentState {
version:number
vspace:vscode.Position|undefined
ruinedUndoStopPosition:boolean=false
redos:Array<Array<[number,number,string]>>=[]
perturbedRedoStack:boolean=false
column:number|undefined
ruinedColumnHiddenState:boolean=false
constructor(version:number) {
this.version=version
}
resetVspace() {
this.vspace=undefined
this.ruinedUndoStopPosition=false
}
resetColumn() {
this.column=undefined
this.ruinedColumnHiddenState=false
Expand All @@ -45,6 +50,8 @@ export function activate(context: vscode.ExtensionContext) {
/*
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
// maybe could force focus, but that's unexpected behavior
// there's also 'Save All' then some documents are surely not in focus
// 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))
Expand Down Expand Up @@ -109,6 +116,7 @@ async function onDidChangeTextEditorSelectionListener(event:vscode.TextEditorSel

async function onDidSaveTextDocumentListener(document:vscode.TextDocument) {
const state=getDocumentState(document)
state.ruinedUndoStopPosition=true
if (!state.vspace) return
vscode.window.showInformationMessage(
`Saved ${document.fileName} with fake virtual space`,
Expand Down Expand Up @@ -156,6 +164,11 @@ async function cursorHorizontalMove(moveCommand:string,moveDelta:number) {
const text=editor.document.lineAt(position).text
const state=getDocumentState(editor.document)
state.resetColumn()
if (state.vspace && state.ruinedUndoStopPosition && position.character>=text.length) {
const vspaceText=text.substr(state.vspace.character)
await cleanupVspace(editor)
await doVspace(editor,vspaceText)
}
if (moveDelta>0 && position.character<text.length) {
await vscode.commands.executeCommand(moveCommand)
return
Expand Down Expand Up @@ -338,7 +351,7 @@ async function cursorVerticalMoveWithWordWrap(editor:vscode.TextEditor,moveComma
async function cleanupVspace(editor:vscode.TextEditor) {
const state=getDocumentState(editor.document)
if (!state.vspace) return
state.vspace=undefined
state.resetVspace()
await undoKeepingSelection(editor)
state.version=editor.document.version
state.ruinedColumnHiddenState=true
Expand All @@ -349,7 +362,7 @@ async function undoVspaceIfNotInside(editor:vscode.TextEditor) {
if (!state.vspace) return
const position=editor.selection.active
if (position.line==state.vspace.line && position.character>=state.vspace.character) return
state.vspace=undefined
state.resetVspace()
await undoKeepingSelection(editor)
state.version=editor.document.version
state.ruinedColumnHiddenState=true
Expand Down Expand Up @@ -388,7 +401,7 @@ async function undo() {
const state=getDocumentState(editor.document)
if (state.vspace) {
await vscode.commands.executeCommand('undo')
state.vspace=undefined
state.resetVspace()
state.resetColumn()
state.version=editor.document.version
}
Expand Down Expand Up @@ -432,7 +445,7 @@ async function redo() {
if (state.perturbedRedoStack && redo) {
if (state.vspace) {
await vscode.commands.executeCommand('undo')
state.vspace=undefined
state.resetVspace()
state.resetColumn()
}
await doRecordedRedo(editor,redo)
Expand Down
27 changes: 27 additions & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,33 @@ suite("Extension Test Suite for normal settings",()=>{
await vscode.commands.executeCommand("workbench.action.closeActiveEditor")
}
})
test("handles horizontal vspace changes after save",async()=>{
const filename=`${__dirname}/vspace-save-and-horizontal-move-test.txt`
fs.writeFileSync(filename,"\n")
try {
const document=await vscode.workspace.openTextDocument(filename)
const editor=await vscode.window.showTextDocument(document)
try {
const startPosition=new vscode.Position(0,0)
editor.selection=new vscode.Selection(startPosition,startPosition)
assert.equal(document.getText(),"\n")
await vscode.commands.executeCommand("fakeVirtualSpace.cursorRight")
assert.equal(document.getText()," \n")
await vscode.commands.executeCommand("fakeVirtualSpace.cursorRight")
assert.equal(document.getText()," \n")
await document.save()
assert.equal(document.getText()," \n")
await vscode.commands.executeCommand("fakeVirtualSpace.cursorLeft")
assert.equal(document.getText()," \n")
await vscode.commands.executeCommand("fakeVirtualSpace.cursorDown")
assert.equal(document.getText(),"\n ")
} finally {
await vscode.commands.executeCommand("workbench.action.closeActiveEditor")
}
} finally {
fs.unlinkSync(filename)
}
})
/* making these work is too much trouble
test("cleans up vspace on save",async()=>{
const filename=`${__dirname}/vspace-save-cleanup-test.txt`
Expand Down

0 comments on commit 61a7a7d

Please sign in to comment.