-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move extended language formatting to client (#1258)
- Loading branch information
Showing
6 changed files
with
191 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { diffChars } from "diff"; | ||
import * as fs from "fs/promises"; | ||
import * as path from "path"; | ||
import { spawnSync } from "child_process"; | ||
import * as vscode from "vscode"; | ||
import { getDenoCommandName } from "./util"; | ||
|
||
const SUPPORTED_EXTENSIONS_BY_LANGUAGE_ID = new Map([ | ||
["jsonc", "jsonc"], | ||
["markdown", "markdown"], | ||
["html", "html"], | ||
["css", "css"], | ||
["scss", "scss"], | ||
["sass", "sass"], | ||
["less", "less"], | ||
["yaml", "yaml"], | ||
["sql", "sql"], | ||
["svelte", "svelte"], | ||
["vue", "vue"], | ||
["astro", "astro"], | ||
["vento", "vto"], | ||
["nunjucks", "njk"], | ||
]); | ||
|
||
export const DENO_FORMATTING_EDIT_PROVIDER: | ||
vscode.DocumentFormattingEditProvider = { | ||
async provideDocumentFormattingEdits(document, options, _token) { | ||
// TODO(nayeemrmn): Account for `deno.json` exclude settings. | ||
const ext = SUPPORTED_EXTENSIONS_BY_LANGUAGE_ID.get(document.languageId); | ||
if (!ext) { | ||
throw new Error("Unexpected language ID for client-side formatting."); | ||
} | ||
const command = await getDenoCommandName(); | ||
let cwd = path.dirname(document.uri.fsPath); | ||
while (!isDir(cwd)) { | ||
const parentDir = path.dirname(cwd); | ||
if (parentDir == cwd) { | ||
break; | ||
} | ||
cwd = parentDir; | ||
} | ||
const configArgs = []; | ||
if (!options.insertSpaces) { | ||
configArgs.push("--use-tabs"); | ||
} | ||
configArgs.push("--indent-width"); | ||
configArgs.push(options.tabSize.toString()); | ||
const input = document.getText(); | ||
const { stdout, error } = spawnSync(command, [ | ||
"fmt", | ||
"--unstable-component", | ||
"--unstable-sql", | ||
"--ext", | ||
ext, | ||
...configArgs, | ||
"-", | ||
], { | ||
encoding: "utf-8", | ||
cwd, | ||
stdio: "pipe", | ||
input, | ||
}); | ||
if (error) { | ||
throw error; | ||
} | ||
const edits = []; | ||
let currentOffset = 0; | ||
for (const change of diffChars(input, stdout)) { | ||
if (change.added) { | ||
edits.push( | ||
vscode.TextEdit.insert( | ||
document.positionAt(currentOffset), | ||
change.value, | ||
), | ||
); | ||
} else { | ||
const nextOffset = currentOffset + (change.count ?? 0); | ||
if (change.removed) { | ||
edits.push( | ||
vscode.TextEdit.delete( | ||
new vscode.Range( | ||
document.positionAt(currentOffset), | ||
document.positionAt(nextOffset), | ||
), | ||
), | ||
); | ||
} | ||
currentOffset = nextOffset; | ||
} | ||
} | ||
return edits; | ||
}, | ||
}; | ||
|
||
async function isDir(path: string): Promise<boolean> { | ||
try { | ||
const stats = await fs.stat(path); | ||
return stats.isDirectory(); | ||
} catch { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters