Skip to content

Commit

Permalink
style(prettier): format with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
DomiR committed Apr 1, 2018
1 parent 7286eea commit 8f3d19a
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 408 deletions.
18 changes: 12 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ import { saveWithoutReplacing, regreplaceCurrentDocument, runSingleRule } from '
import { EXTENSION_NAME } from './utils';

export function activate({ subscriptions }: ExtensionContext) {
subscriptions.push(commands.registerCommand(EXTENSION_NAME + '.regreplace', regreplaceCurrentDocument));
subscriptions.push(commands.registerCommand(EXTENSION_NAME + '.save-without-regreplace', saveWithoutReplacing));
subscriptions.push(commands.registerCommand(EXTENSION_NAME + '.run-single-rule', runSingleRule));
subscriptions.push(
commands.registerCommand(EXTENSION_NAME + '.regreplace', regreplaceCurrentDocument),
);
subscriptions.push(
commands.registerCommand(EXTENSION_NAME + '.save-without-regreplace', saveWithoutReplacing),
);
subscriptions.push(
commands.registerCommand(EXTENSION_NAME + '.run-single-rule', runSingleRule),
);

onSave.update();
workspace.onDidChangeConfiguration(() => onSave.update());
onSave.update();
workspace.onDidChangeConfiguration(() => onSave.update());
}

export function deactivate() { }
export function deactivate() {}
139 changes: 74 additions & 65 deletions src/on-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,96 @@
* @version 0.0.1
*/

import { Disposable, TextDocumentWillSaveEvent, TextEdit, workspace, window, Position, Selection } from 'vscode';
import {
Disposable,
TextDocumentWillSaveEvent,
TextEdit,
workspace,
window,
Position,
Selection,
} from 'vscode';
import { calculateTargetTextForAllRules, getCustomEdits, CustomEditType } from './regreplace';
import { getConfiguration, getMaxRange } from './utils';
import { getConfiguration, getMaxRange } from './utils';

let subscription: Disposable;
export default {
get isEnabled() {
return getConfiguration<boolean>('on-save');
},

get isEnabled() {
return getConfiguration<boolean>('on-save');
},
register() {
if (subscription) {
return;
}

register() {
if (subscription) {
return;
}
subscription = workspace.onWillSaveTextDocument(listener);
},

subscription = workspace.onWillSaveTextDocument(listener);
},
unregister() {
if (!subscription) {
return;
}

unregister() {
if (!subscription) {
return;
}
subscription.dispose();
subscription = null;
},

subscription.dispose();
subscription = null;
},
update() {
if (this.isEnabled) {
this.register();
} else {
this.unregister();
}
},

update() {
if (this.isEnabled) {
this.register();
} else {
this.unregister();
}
},

bypass(action) {
this.unregister();
const result = action();
return result.then(() => this.update());
}
bypass(action) {
this.unregister();
const result = action();
return result.then(() => this.update());
},
};

/**
* callback listener, we apply small delta changes
*/
function listener({ document, waitUntil }: TextDocumentWillSaveEvent) {
const regreplacedText = calculateTargetTextForAllRules(document);
if (!regreplacedText || regreplacedText === document.getText()) {
return;
}

const regreplacedText = calculateTargetTextForAllRules(document);
if (!regreplacedText || regreplacedText === document.getText()) {
return;
}

// v1 use diff edits
const edits = getCustomEdits(document.getText(), regreplacedText);
const textEdits = edits.map(e => {
switch (e.action) {
case CustomEditType.Replace: return new TextEdit(e.range, e.value);
case CustomEditType.Insert: return new TextEdit(e.range, e.value);
case CustomEditType.Delete: return new TextEdit(e.range, '');
}
})
waitUntil(Promise.all(textEdits))
// v1 use diff edits
const edits = getCustomEdits(document.getText(), regreplacedText);
const textEdits = edits.map(e => {
switch (e.action) {
case CustomEditType.Replace:
return new TextEdit(e.range, e.value);
case CustomEditType.Insert:
return new TextEdit(e.range, e.value);
case CustomEditType.Delete:
return new TextEdit(e.range, '');
}
});
waitUntil(Promise.all(textEdits));

// v0 use replace all
// let selections: Selection[];
// if (window.activeTextEditor.document === document) {
// selections = window.activeTextEditor.selections;
// }
// const edits = Promise.resolve([ new TextEdit(getMaxRange(), regreplacedText) ]);
// waitUntil(edits);
// if (selections && selections.length >= 1) {
// edits.then(() => {
// const selection = selections[0];
// if (window.activeTextEditor.document.lineCount > selection.start.line) {
// const currentLine = window.activeTextEditor.document.lineAt(selection.start.line);
// const currentCharacter = Math.min(currentLine.text.length, selection.start.character);
// const pos = new Position(currentLine.lineNumber, currentCharacter);
// const newSelection = new Selection(pos, pos);
// window.activeTextEditor.selection = newSelection;
// }
// });
// }
// v0 use replace all
// let selections: Selection[];
// if (window.activeTextEditor.document === document) {
// selections = window.activeTextEditor.selections;
// }
// const edits = Promise.resolve([ new TextEdit(getMaxRange(), regreplacedText) ]);
// waitUntil(edits);
// if (selections && selections.length >= 1) {
// edits.then(() => {
// const selection = selections[0];
// if (window.activeTextEditor.document.lineCount > selection.start.line) {
// const currentLine = window.activeTextEditor.document.lineAt(selection.start.line);
// const currentCharacter = Math.min(currentLine.text.length, selection.start.character);
// const pos = new Position(currentLine.lineNumber, currentCharacter);
// const newSelection = new Selection(pos, pos);
// window.activeTextEditor.selection = newSelection;
// }
// });
// }
}
Loading

0 comments on commit 8f3d19a

Please sign in to comment.