Skip to content

Added action replace #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/CommandAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ export default class CommandAction implements Action {
]): Promise<ActionReturnValue> {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.referenced,
this.graph.editStyles.referencedLine
this.graph.editStyles.referenced
);

const originalEditor = window.activeTextEditor;
Expand Down
3 changes: 2 additions & 1 deletion src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SyntaxNode } from "web-tree-sitter";
import * as vscode from "vscode";
import { Location } from "vscode";
import { SymbolColor } from "./constants";
import EditStyles from "./editStyles";
import { EditStyles } from "./editStyles";
import NavigationMap from "./NavigationMap";

/**
Expand Down Expand Up @@ -256,6 +256,7 @@ export type ActionType =
| "move"
| "outdentLines"
| "paste"
| "replaceWithText"
| "scrollToBottom"
| "scrollToCenter"
| "scrollToTop"
Expand Down
85 changes: 29 additions & 56 deletions src/actions/BringMoveSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ import {
import { runForEachEditor } from "../targetUtils";
import update from "immutability-helper";
import displayPendingEditDecorations from "../editDisplayUtils";
import { performInsideOutsideAdjustment } from "../performInsideOutsideAdjustment";
import { performOutsideAdjustment } from "../performInsideOutsideAdjustment";
import { flatten, zip } from "lodash";
import { Selection, TextEditorDecorationType, TextEditor, Range } from "vscode";
import { Selection, TextEditor, Range } from "vscode";
import { performEditsAndUpdateSelections } from "../updateSelections";

interface DecorationTypes {
sourceStyle: TextEditorDecorationType;
sourceLineStyle: TextEditorDecorationType;
destinationStyle: TextEditorDecorationType;
destinationLineStyle: TextEditorDecorationType;
}
import { getTextWithPossibleDelimiter } from "../getTextWithPossibleDelimiter";

interface ExtendedEdit extends Edit {
editor: TextEditor;
Expand All @@ -37,7 +31,7 @@ interface ThatMarkEntry {
class BringMoveSwap implements Action {
targetPreferences: ActionPreferences[] = [
{ insideOutsideType: null },
{ insideOutsideType: "inside" },
{ insideOutsideType: null },
];

constructor(private graph: Graph, private type: string) {
Expand All @@ -56,25 +50,20 @@ class BringMoveSwap implements Action {
return sources;
}

private getDecorationStyles(): DecorationTypes {
let sourceStyle, sourceLineStyle;
private getDecorationStyles() {
let sourceStyle;
if (this.type === "bring") {
sourceStyle = this.graph.editStyles.referenced;
sourceLineStyle = this.graph.editStyles.referencedLine;
} else if (this.type === "swap") {
sourceStyle = this.graph.editStyles.pendingModification1;
sourceLineStyle = this.graph.editStyles.pendingLineModification1;
} else if (this.type === "move") {
sourceStyle = this.graph.editStyles.pendingDelete;
sourceLineStyle = this.graph.editStyles.pendingLineDelete;
} else {
throw Error(`Unknown type "${this.type}"`);
}
// NB this.type === "swap"
else {
sourceStyle = this.graph.editStyles.pendingModification1;
}
return {
sourceStyle,
sourceLineStyle,
destinationStyle: this.graph.editStyles.pendingModification0,
destinationLineStyle: this.graph.editStyles.pendingLineModification0,
};
}

Expand All @@ -84,15 +73,10 @@ class BringMoveSwap implements Action {
) {
const decorationTypes = this.getDecorationStyles();
await Promise.all([
displayPendingEditDecorations(
sources,
decorationTypes.sourceStyle,
decorationTypes.sourceLineStyle
),
displayPendingEditDecorations(sources, decorationTypes.sourceStyle),
displayPendingEditDecorations(
destinations,
decorationTypes.destinationStyle,
decorationTypes.destinationLineStyle
decorationTypes.destinationStyle
),
]);
}
Expand All @@ -107,54 +91,45 @@ class BringMoveSwap implements Action {
throw new Error("Targets must have same number of args");
}

const sourceText = source.selection.editor.document.getText(
source.selection.selection
);

const { containingListDelimiter } = destination.selectionContext;
const newText =
containingListDelimiter == null || destination.position === "contents"
? sourceText
: destination.position === "after"
? containingListDelimiter + sourceText
: sourceText + containingListDelimiter;
// Get text adjusting for destination position
const text = getTextWithPossibleDelimiter(source, destination);

// Add destination edit
const result = [
{
isSource: false,
range: destination.selection.selection as Range,
text,
editor: destination.selection.editor,
originalSelection: destination,
range: destination.selection.selection as Range,
text: newText,
isSource: false,
},
];

// Add source edit for move and swap
// Prevent multiple instances of the same expanded source.
if (this.type !== "bring" && !usedSources.includes(source)) {
let newText: string;
let text: string;
let range: Range;

if (this.type === "swap") {
newText = destination.selection.editor.document.getText(
text = destination.selection.editor.document.getText(
destination.selection.selection
);
range = source.selection.selection;
} else {
// NB: this.type === "move"
newText = "";
range = performInsideOutsideAdjustment(source, "outside").selection
.selection;
}
// NB: this.type === "move"
else {
text = "";
range = performOutsideAdjustment(source).selection.selection;
}

usedSources.push(source);
result.push({
isSource: true,
range,
text,
editor: source.selection.editor,
originalSelection: source,
range,
text: newText,
isSource: true,
});
}

Expand Down Expand Up @@ -206,15 +181,13 @@ class BringMoveSwap implements Action {
thatMark
.filter(({ isSource }) => isSource)
.map(({ typedSelection }) => typedSelection),
decorationTypes.sourceStyle,
decorationTypes.sourceLineStyle
decorationTypes.sourceStyle
),
displayPendingEditDecorations(
thatMark
.filter(({ isSource }) => !isSource)
.map(({ typedSelection }) => typedSelection),
decorationTypes.destinationStyle,
decorationTypes.destinationLineStyle
decorationTypes.destinationStyle
),
]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/actions/CopyLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class CopyLines implements Action {
async run([targets]: [TypedSelection[]]): Promise<ActionReturnValue> {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.referenced,
this.graph.editStyles.referencedLine
this.graph.editStyles.referenced
);

const thatMark = flatten(
Expand Down
10 changes: 7 additions & 3 deletions src/actions/Find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ export class FindInFiles implements Action {
async run([targets]: [TypedSelection[]]): Promise<ActionReturnValue> {
ensureSingleTarget(targets);

const { returnValue: query, thatMark } =
await this.graph.actions.getText.run([targets]);
const {
returnValue: [query],
thatMark,
} = await this.graph.actions.getText.run([targets]);

await commands.executeCommand("workbench.action.findInFiles", { query });
await commands.executeCommand("workbench.action.findInFiles", {
query,
});

return { returnValue: null, thatMark };
}
Expand Down
15 changes: 6 additions & 9 deletions src/actions/GetText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@ export default class GetText implements Action {

async run(
[targets]: [TypedSelection[]],
showDecorations = true
{ showDecorations = true } = {}
): Promise<ActionReturnValue> {
if (showDecorations) {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.referenced,
this.graph.editStyles.referencedLine
this.graph.editStyles.referenced
);
}

const text = targets
.map((target) =>
target.selection.editor.document.getText(target.selection.selection)
)
.join("\n");
const returnValue = targets.map((target) =>
target.selection.editor.document.getText(target.selection.selection)
);

return {
returnValue: text,
returnValue,
thatMark: targets.map((target) => target.selection),
};
}
Expand Down
1 change: 0 additions & 1 deletion src/actions/InsertEmptyLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class InsertEmptyLines implements Action {
displayPendingEditDecorations(
targets,
this.graph.editStyles.referenced,
this.graph.editStyles.referencedLine
);

const edits = await runForEachEditor(
Expand Down
2 changes: 0 additions & 2 deletions src/actions/Paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export default class Paste implements Action {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.pendingModification0,
this.graph.editStyles.pendingLineModification0
);

const text = await env.clipboard.readText();
Expand Down Expand Up @@ -78,7 +77,6 @@ export default class Paste implements Action {
await displayPendingEditDecorations(
thatMark.map(({ typedSelection }) => typedSelection),
this.graph.editStyles.pendingModification0,
this.graph.editStyles.pendingLineModification0
);

return { returnValue: null, thatMark };
Expand Down
66 changes: 66 additions & 0 deletions src/actions/ReplaceWithText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
} from "../Types";
import displayPendingEditDecorations from "../editDisplayUtils";
import { runForEachEditor } from "../targetUtils";
import { flatten, zip } from "lodash";
import { maybeAddDelimiter } from "../getTextWithPossibleDelimiter";
import { performEditsAndUpdateSelections } from "../updateSelections";

export default class implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: null }];

constructor(private graph: Graph) {
this.run = this.run.bind(this);
}

async run(
[targets]: [TypedSelection[]],
texts: string[]
): Promise<ActionReturnValue> {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.pendingModification0
);

// Broadcast single text for each target
if (texts.length === 1) {
texts = Array(targets.length).fill(texts[0]);
}

if (targets.length !== texts.length) {
throw new Error("Targets and texts must have same length");
}

const edits = zip(targets, texts).map(([target, text]) => ({
editor: target!.selection.editor,
range: target!.selection.selection,
text: maybeAddDelimiter(text!, target!),
}));

const thatMark = flatten(
await runForEachEditor(
edits,
(edit) => edit.editor,
async (editor, edits) => {
const [updatedSelections] = await performEditsAndUpdateSelections(
editor,
edits,
[targets.map((target) => target.selection.selection)]
);

return updatedSelections.map((selection) => ({
editor,
selection,
}));
}
)
);

return { returnValue: null, thatMark };
}
}
2 changes: 1 addition & 1 deletion src/actions/Scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Scroll implements Action {

await displayDecorationsWhileRunningFunc(
targets.map((target) => target.selection),
this.graph.editStyles.referencedLine,
this.graph.editStyles.referenced.line,
scrollCallback,
showAdditionalHighlightBeforeScroll
);
Expand Down
3 changes: 1 addition & 2 deletions src/actions/SetBreakpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export default class SetBreakpoint implements Action {
]): Promise<ActionReturnValue> {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.referenced,
this.graph.editStyles.referencedLine
this.graph.editStyles.referenced
);

const lines = targets.flatMap((target) => {
Expand Down
3 changes: 2 additions & 1 deletion src/actions/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export default class Copy implements Action {
const { returnValue, thatMark } = await this.graph.actions.getText.run([
targets,
]);
const text = returnValue.join("\n");

await env.clipboard.writeText(returnValue);
await env.clipboard.writeText(text);

return { returnValue: null, thatMark };
}
Expand Down
Loading