Skip to content

Commit

Permalink
#108 🐛 Fix Windows issues
Browse files Browse the repository at this point in the history
* ♻️ Move ob to central TS file

* #108 🐛 Fix Windows issues

by removing clipboardy and using the built-in copy/cut commands
  • Loading branch information
sansarip authored Jun 5, 2023
1 parent 364b1e2 commit cddd163
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
53 changes: 39 additions & 14 deletions src/ts/extension/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import {
workspace,
} from "vscode";
import { EditCtx, OwlbearFunction } from "./types";
import { getDocCtx, edit, isEmptyObj } from "./utilities";
import clipboard from "clipboardy";
import {
getDocCtx,
edit,
isEmptyObj,
copyRangeToClipboard,
cutRangeToClipboard,
moveCursor,
} from "./utilities";
import { docUriToTreeIdMap, setNewTreeIdForDocUri } from "./tree";
import {
AUTOFORMAT_NAMESPACE,
PAREDIT_NAMESPACE,
setContextFromConfig,
} from "./config";

const ob = require("../../../out/cljs/owlbear");
import ob from "./ob";

type Handler = (editCtx?: EditCtx) => undefined | Thenable<EditCtx | undefined>;

Expand All @@ -41,6 +46,11 @@ type OwlbearOperation =
| "Splice"
| "UpwardMove";

enum ClipboardOp {
copy = "Copy",
cut = "Cut",
}

const getOwlbearFunction = (
operation: OwlbearOperation
): OwlbearFunction | undefined => {
Expand Down Expand Up @@ -96,6 +106,21 @@ const doEditOp: Edit = (obOp: OwlbearOperation) => {
return edit(editor, editCtx, shouldFormat);
};

const doClipboardOp = async (op: ClipboardOp) => {
const ctx = getEditCtx("Kill");
const removedText = ctx?.removedText;
const editor = window.activeTextEditor;
if (!removedText || !editor) {
return;
}
const startIndex = ctx.offset;
const endIndex = startIndex + removedText.length;
const clipboardOpFn =
op === ClipboardOp.copy ? copyRangeToClipboard : cutRangeToClipboard;
await clipboardOpFn(editor, startIndex, endIndex);
return ctx;
};

const deleteLeft = (): void => {
vscodeCommands.executeCommand("deleteLeft");
};
Expand Down Expand Up @@ -170,19 +195,19 @@ const forwardMove: Handler = () => doEditOp("ForwardMove");

const kill: Handler = () => doEditOp("Kill");

const copy: Handler = async (editCtx = undefined) => {
const ctx = editCtx ?? getEditCtx("Kill");
const removedText = ctx?.removedText;
if (!removedText) {
return;
}
clipboard.writeSync(ctx.removedText);
return ctx;
const copy: Handler = async () => {
return doClipboardOp(ClipboardOp.copy);
};

const cut: Handler = async () => {
const editCtx = await kill();
return await copy(editCtx);
const ctx = await doClipboardOp(ClipboardOp.cut);
const editor = window.activeTextEditor;
if (!ctx?.removedText || !editor) {
return;
}
const startIndex = ctx.offset;
moveCursor(editor, startIndex);
return ctx;
};

const raise: Handler = () => {
Expand Down
9 changes: 6 additions & 3 deletions src/ts/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { setContexts } from "./config";
import { deleteTree, editTree } from "./tree";
import { localTimeNow, log, makePath } from "./utilities";
import { owlbearAscii } from "./constants";

const ob = require("../../../out/cljs/owlbear");
import ob from "./ob";

const logWasmLoadingErr = (err: any, lang: string) => {
log(`Error loading WASM for ${lang}: ${err}`);
Expand Down Expand Up @@ -39,7 +38,11 @@ const loadWasms = async (context: vscode.ExtensionContext) => {
try {
await ob.loadLanguageWasm(
ob.tsLangId,
makePath(context.extensionPath, "resources", "tree-sitter-typescript.wasm")
makePath(
context.extensionPath,
"resources",
"tree-sitter-typescript.wasm"
)
);
} catch (err) {
logWasmLoadingErr(err, ob.tsLangId);
Expand Down
2 changes: 2 additions & 0 deletions src/ts/extension/ob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const ob = require("../../../out/cljs/owlbear");
export default ob;
2 changes: 1 addition & 1 deletion src/ts/extension/tree.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { randomUUID } from "crypto";
import { TextDocumentChangeEvent, TextDocument } from "vscode";
import { asPoint } from "./utilities";
import ob from "./ob";

const ob = require("../../../out/cljs/owlbear");
export const docUriToTreeIdMap: { [key: string]: string } = {};

export const setNewTreeIdForDocUri = (docUri: string): string => {
Expand Down
30 changes: 30 additions & 0 deletions src/ts/extension/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,33 @@ export const isEmptyObj = (obj: object): boolean =>
Object.keys(obj).length === 0;

export const localTimeNow = (): string => new Date().toLocaleTimeString();

export const selectRange = (
editor: TextEditor,
startIndex: number,
endIndex: number
): void => {
const document = editor.document;
const startPosition = document.positionAt(startIndex);
const endPosition = document.positionAt(endIndex);
const customSelection = new Selection(startPosition, endPosition);
editor.selection = customSelection;
};

export const copyRangeToClipboard = async (
editor: TextEditor,
startIndex: number,
endIndex: number
): Promise<void> => {
selectRange(editor, startIndex, endIndex);
await commands.executeCommand("editor.action.clipboardCopyAction");
};

export const cutRangeToClipboard = async (
editor: TextEditor,
startIndex: number,
endIndex: number
): Promise<void> => {
selectRange(editor, startIndex, endIndex);
await commands.executeCommand("editor.action.clipboardCutAction");
};

0 comments on commit cddd163

Please sign in to comment.