Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
feat: support format range code
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Feb 29, 2020
1 parent 61bce12 commit 498d37f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion server/src/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Deno {
let stdout = "";
let stderr = "";
subprocess.on("exit", (exitCode: number) => {
if (exitCode != 0) {
if (exitCode !== 0 || stderr) {
reject(new Error(stderr));
} else {
resolve(stdout);
Expand Down
32 changes: 23 additions & 9 deletions server/src/language/document_formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ export class DocumentFormatting {

const cwd = workspaceFolder ? workspaceFolder.uri.fsPath : "./";

connection.console.log(
`Formatting '${uri.toString()}' at ${
workspaceFolder ? workspaceFolder.uri.fsPath : ""
}`
);

const formatted = await deno.format(text, {
cwd
});
const formatted = await deno.format(text, { cwd });

const start = doc.positionAt(0);
const end = doc.positionAt(text.length);
Expand All @@ -46,5 +38,27 @@ export class DocumentFormatting {

return [TextEdit.replace(range, formatted)];
});

connection.onDocumentRangeFormatting(async params => {
const uri = params.textDocument.uri;
const range = params.range;
const doc = documents.get(uri);

if (!doc) {
return;
}

const text = doc.getText(range);

const workspaceFolder = await bridge.getWorkspace(uri);

const cwd = workspaceFolder ? workspaceFolder.uri.fsPath : "./";

const formatted = await deno.format(text, { cwd });

// why trim it?
// Because we are just formatting some of them, we don't need to keep the trailing \n
return [TextEdit.replace(range, formatted.trim())];
});
}
}
1 change: 1 addition & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ connection.onInitialize(
return {
capabilities: {
documentFormattingProvider: true,
documentRangeFormattingProvider: true,
textDocumentSync: {
openClose: true,
change: TextDocumentSyncKind.Full
Expand Down

0 comments on commit 498d37f

Please sign in to comment.