vscode#DocumentFormattingEditProvider TypeScript Examples
The following examples show how to use
vscode#DocumentFormattingEditProvider.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: cfgFormat.ts From sourcepawn-vscode with MIT License | 6 votes |
export class CFGDocumentFormattingEditProvider
implements DocumentFormattingEditProvider {
public provideDocumentFormattingEdits(
document: TextDocument,
options: FormattingOptions,
token: CancellationToken
): ProviderResult<TextEdit[]> {
let workspaceFolder = Workspace.getWorkspaceFolder(document.uri);
// Get the user's settings.
let insertSpaces: boolean =
Workspace.getConfiguration("editor", workspaceFolder).get(
"insertSpaces"
) || false;
let tabSize: number =
Workspace.getConfiguration("editor", workspaceFolder).get("tabSize") || 2;
// Apply user settings
const start = new Position(0, 0);
const end = new Position(
document.lineCount - 1,
document.lineAt(document.lineCount - 1).text.length
);
let range = new Range(start, end);
let text = formatCFGText(document.getText(), insertSpaces, tabSize);
// If process failed,
if (text === "") {
window.showErrorMessage(
"The formatter failed to run, check the console for more details."
);
return undefined;
}
return [new TextEdit(range, text)];
}
}
Example #2
Source File: extension.ts From vscode-crestron-splus with GNU General Public License v3.0 | 6 votes |
export class formattingProvider
implements
DocumentRangeFormattingEditProvider,
DocumentFormattingEditProvider {
constructor(
private provideEdits: (
document: TextDocument,
options?: RangeFormattingOptions
) => Promise<TextEdit[]>
) { }
public async provideDocumentRangeFormattingEdits(
document: TextDocument,
range: Range,
_options: FormattingOptions,
_token: CancellationToken
): Promise<TextEdit[]> {
return this.provideEdits(document, {
rangeEnd: document.offsetAt(range.end),
rangeStart: document.offsetAt(range.start),
});
}
public async provideDocumentFormattingEdits(
document: TextDocument,
_options: FormattingOptions,
_token: CancellationToken
): Promise<TextEdit[]> {
return this.provideEdits(document);
}
}
Example #3
Source File: spFormat.ts From sourcepawn-vscode with MIT License | 4 votes |
export class SMDocumentFormattingEditProvider
implements DocumentFormattingEditProvider {
public provideDocumentFormattingEdits(
document: TextDocument,
options: FormattingOptions,
token: CancellationToken
): ProviderResult<TextEdit[]> {
// Get the user's settings.
let insertSpaces: boolean =
Workspace.getConfiguration("editor").get("insertSpaces") || false;
let UseTab: string = insertSpaces ? "Never" : "Always";
let tabSize: number =
Workspace.getConfiguration("editor").get("tabSize") || 2;
let workspaceFolder = Workspace.getWorkspaceFolder(document.uri);
let defaultStyles: string[] =
Workspace.getConfiguration("sourcepawn", workspaceFolder).get(
"formatterSettings"
) || [];
let default_style: string = "{" + defaultStyles.join(", ") + "}";
// Apply user settings
default_style = default_style
.replace(/\${TabSize}/g, tabSize.toString())
.replace(/\${UseTab}/g, UseTab);
const start = new Position(0, 0);
const end = new Position(
document.lineCount - 1,
document.lineAt(document.lineCount - 1).text.length
);
const range = new Range(start, end);
const tempFile = join(__dirname, "temp_format.sp");
let file = openSync(tempFile, "w", 0o765);
writeSync(file, document.getText());
closeSync(file);
let text = this.clangFormat(tempFile, "utf-8", default_style);
// If process failed,
if (text === undefined) {
window.showErrorMessage(
"The formatter failed to run, check the console for more details."
);
return undefined;
}
text = fixFormatting(text);
return [new TextEdit(range, text)];
}
Callback(e) {
console.error(e);
}
clangFormat(path: string, enc: string, style): string | undefined {
let args = [`-style=${style}`, path];
let result = this.spawnClangFormat(args, [
"ignore",
"pipe",
process.stderr,
]);
if (result) {
return result;
} else {
console.error("Formatting failed.");
return undefined;
}
}
spawnClangFormat(args, stdio) {
let nativeBinary;
try {
nativeBinary = this.getNativeBinary();
} catch (e) {
return undefined;
}
try {
let clangFormatProcess = execFileSync(nativeBinary, args);
return clangFormatProcess.toString();
} catch (e) {
console.error("Error", e);
return undefined;
}
}
getNativeBinary() {
let nativeBinary;
const sysPlatform = platform();
const sysArch = arch();
const ext = extensions.getExtension("Sarrus.sourcepawn-vscode");
if (ext === undefined) {
throw Error("Extension not found.");
}
const myExtDir = ext.extensionPath;
if (sysPlatform === "win32") {
nativeBinary = join(myExtDir, "/bin/win32/clang-format.exe");
} else {
nativeBinary = join(
myExtDir,
`/bin/${sysPlatform}_${sysArch}/clang-format`
);
}
if (existsSync(nativeBinary)) {
return nativeBinary;
}
// Let arm64 macOS fall back to x64
if (sysPlatform === "darwin" && sysArch === "arm64") {
nativeBinary = join(myExtDir, `/bin/darwin_x64/clang-format`);
if (existsSync(nativeBinary)) {
return nativeBinary;
}
}
const message =
"This module doesn't bundle the clang-format executable for your platform. " +
`(${sysPlatform}_${sysArch})\n` +
"Please let the author know on GitHub.\n";
throw new Error(message);
}
}