Skip to content

Commit

Permalink
improve code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
callmephilip committed Sep 11, 2024
1 parent 0673624 commit 915f009
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@
// Import the module and reference it with the alias vscode in your code below
import * as vscode from "vscode";
import { html2ft } from "./converter";
import { indent } from "./helpers";

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated

// XX: keeping this here for posterity
// const getCurrentSelectionStartCol = (editor: vscode.TextEditor) => {
// // re: https://stackoverflow.com/questions/64791832/vscode-how-can-i-determine-the-cursors-column-position-within-an-editor
// let offset = editor.document.offsetAt(editor.selection.start);
// const line = editor.selection.start.line;
// for (let i = 0; i < line; i++) {
// offset -= editor.document.lineAt(i).text.length + 1;
// }
// return offset;
// };

const countSpaces = (editor: vscode.TextEditor) => {
return editor.document
.lineAt(editor.selection.start.line)
.text.search(/\S/);
};

context.subscriptions.push(
vscode.commands.registerCommand("html2ft.convert", () => {
const editor = vscode.window.activeTextEditor;

if (!editor) {
return;
}
Expand All @@ -21,10 +40,14 @@ export function activate(context: vscode.ExtensionContext) {
const originalText = document.getText(selection);

try {
const convertedText = html2ft(
originalText,
vscode.workspace.getConfiguration("html2ft").get("attrs1st")
const convertedText = indent(
html2ft(
originalText,
vscode.workspace.getConfiguration("html2ft").get("attrs1st")
),
countSpaces(editor)
);

editor.edit((editBuilder) => {
editBuilder.replace(selection, convertedText);
});
Expand Down
7 changes: 7 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const indent = (str: string, spaces: number) => {
const space = " ".repeat(spaces);
return str
.split("\n")
.map((line, i) => (i !== 0 ? `${space}${line}` : line))
.join("\n");
};
8 changes: 8 additions & 0 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { describe, expect, it } from "vitest";
import { indent } from "../src/helpers";

describe("indent", () => {
it("works", () => {
expect(indent("foo\nbar", 2)).toBe("foo\n bar");
});
});

0 comments on commit 915f009

Please sign in to comment.