Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ foo bar baz: "foo bar baz"
```yaml
repos:
- repo: https://github.com/cursorless-dev/talon-tools
rev: v0.12.1
rev: v0.13.0
hooks:
- id: talon-fmt
- id: snippet-fmt
Expand Down
4 changes: 2 additions & 2 deletions dist/lib.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/lib.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/snippetFormatter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/snippetFormatter.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/talon/parseTalonList.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface TalonListHeader {
interface TalonListItem {
type: "item";
key: string;
value?: string;
valueLines: string[];
}
interface EmptyLine {
type: "empty";
Expand Down
2 changes: 1 addition & 1 deletion dist/talon/talonListFormatter.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FormatterOptions } from "../types.js";
type Options = FormatterOptions<"endOfLine" | "columnWidth" | "insertFinalNewline">;
type Options = FormatterOptions<"endOfLine" | "indentTabs" | "indentSize" | "columnWidth" | "insertFinalNewline">;
export declare function talonListFormatter(text: string, options?: Options): string;
export {};
16 changes: 8 additions & 8 deletions dist/talonFormatter.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/talonFormatter.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/treeSitterFormatter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/treeSitterFormatter.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cursorless/talon-tools",
"version": "0.12.1",
"version": "0.13.0",
"description": "Linting and formatting tools for Talon and Cursorless",
"author": "Cursorless Dev",
"license": "MIT",
Expand Down
69 changes: 61 additions & 8 deletions src/talon/parseTalonList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ interface TalonListHeader {
interface TalonListItem {
type: "item";
key: string;
value?: string;
// Length 0 = No value and no trailing colon.
// Length 1 = Single line value. Can be an empty string for trailing colon.
// Length > 1 = Multiple line value from an indented block. First line is empty string.
valueLines: string[];
}

interface EmptyLine {
Expand All @@ -25,14 +28,16 @@ export interface TalonList {
}

export function parseTalonList(text: string): TalonList {
const lines = text.split(/\r?\n/u).map((l) => l.trim());
const separatorIndex = lines.indexOf("-");
const lines = text.split(/\r?\n/u);
const separatorIndex = lines.findIndex((line) => /^\s*-\s*$/u.test(line));

if (separatorIndex === -1) {
throw new Error("Separator not found in talon list");
}

const headerLines = lines.slice(0, separatorIndex);
const headerLines = lines
.slice(0, separatorIndex)
.map((line) => line.trim());
const bodyLines = trim(lines.slice(separatorIndex + 1));

const result: TalonList = {
Expand All @@ -55,7 +60,8 @@ export function parseTalonList(text: string): TalonList {
result.headers.push({ type: "header", key, value });
}

for (const line of bodyLines) {
for (let index = 0; index < bodyLines.length; index++) {
const line = bodyLines[index].trim();
if (line.length === 0) {
result.items.push({ type: "empty" });
continue;
Expand All @@ -65,12 +71,59 @@ export function parseTalonList(text: string): TalonList {
continue;
}
const [key, value] = splitLine(line);
result.items.push({ type: "item", key, value });

if (value == null) {
result.items.push({ type: "item", key, valueLines: [] });
continue;
}

const valueLines = [value];

// value === "" indicates that the following lines may be an indented block.
if (value === "") {
const indentedLines = readIndentedLines(bodyLines, index + 1);
index += indentedLines.length;
valueLines.push(...indentedLines);
}

result.items.push({ type: "item", key, valueLines });
}

return result;
}

function readIndentedLines(lines: string[], startIndex: number): string[] {
let firstNonBlankIndex = startIndex;
while (
firstNonBlankIndex < lines.length &&
lines[firstNonBlankIndex].trim().length === 0
) {
firstNonBlankIndex++;
}
const indentation = /^[\t ]+/u.exec(lines[firstNonBlankIndex])?.[0];
Comment thread
AndreasArvidsson marked this conversation as resolved.
Resolved

if (indentation == null) {
return [];
}

const result: string[] = [];

for (let index = startIndex; index < lines.length; index++) {
const line = lines[index];
if (line.trim().length > 0 && !line.startsWith(indentation)) {
break;
Comment thread
AndreasArvidsson marked this conversation as resolved.
Resolved
}
result.push(
index < firstNonBlankIndex ? "" : line.slice(indentation.length),
);
}

// Blank lines after the block belong between items.
result.length = result.findLastIndex((line) => line.trim().length > 0) + 1;

return result;
}

function splitLine(line: string): [string, string | undefined] {
const index = line.indexOf(":");
if (index === -1) {
Expand All @@ -80,10 +133,10 @@ function splitLine(line: string): [string, string | undefined] {
}

function trim(list: string[]): string[] {
const startIndex = list.findIndex((l) => l.length > 0);
const startIndex = list.findIndex((l) => l.trim().length > 0);
if (startIndex === -1) {
return [];
}
const endIndex = list.findLastIndex((l) => l.length > 0);
const endIndex = list.findLastIndex((l) => l.trim().length > 0);
return list.slice(startIndex, endIndex + 1);
}
2 changes: 2 additions & 0 deletions src/talon/talonFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ function forceMultilineBody(node: SyntaxNode): boolean {
case "if_statement":
case "for_statement":
return true;
case "expression_statement":
return node.startPosition.row !== node.endPosition.row;
Comment thread
AndreasArvidsson marked this conversation as resolved.
Resolved
Comment thread
AndreasArvidsson marked this conversation as resolved.
Resolved
default:
return false;
}
Expand Down
39 changes: 31 additions & 8 deletions src/talon/talonListFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import type { FormatterOptions } from "../types.js";
import { DEFAULT_INSERT_FINAL_NEWLINE } from "../util/constants.js";
import { getColumnWidth } from "../util/getColumnWidth.js";
import { getEndOfLine } from "../util/getEndOfLine.js";
import { getIndentation } from "../util/getIndentation.js";
import { convertQuotes } from "./convertQuotes.js";
import { parseTalonList } from "./parseTalonList.js";

type Options = FormatterOptions<
"endOfLine" | "columnWidth" | "insertFinalNewline"
| "endOfLine"
| "indentTabs"
| "indentSize"
| "columnWidth"
| "insertFinalNewline"
>;

export function talonListFormatter(
Expand All @@ -15,6 +20,7 @@ export function talonListFormatter(
): string {
const columnWidth = getColumnWidth(text) ?? options.columnWidth;
const eol = getEndOfLine(options.endOfLine);
const indentation = getIndentation(options.indentTabs, options.indentSize);
const talonList = parseTalonList(text);
talonList.headers.sort((a, _b) =>
a.type === "header" && a.key === "list" ? -1 : 0,
Expand All @@ -40,15 +46,32 @@ export function talonListFormatter(
lines.push(item.text);
continue;
}
if (item.value == null) {
// Empty value lines means there is no colon after the key.
if (item.valueLines.length === 0) {
lines.push(item.key);
} else {
const keyWithColon =
columnWidth == null
? `${item.key}: `
: `${item.key}: `.padEnd(columnWidth);
const value = convertQuotes(item.value);
}
// Non-empty value lines means there is a colon and possibly indented content after the key.
else {
const [firstValue, ...indentedValues] = item.valueLines;
let keyWithColon: string;

if (firstValue === "") {
keyWithColon = `${item.key}:`;
} else {
keyWithColon =
columnWidth == null
? `${item.key}: `
: `${item.key}: `.padEnd(columnWidth);
}
const value = convertQuotes(firstValue);
lines.push(`${keyWithColon}${value}`);
for (const indentedValue of indentedValues) {
lines.push(
indentedValue.length === 0
? ""
: `${indentation}${indentedValue}`,
);
}
}
}

Expand Down
Loading
Loading