-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added external formatter registration
- Loading branch information
Showing
5 changed files
with
92 additions
and
6 deletions.
There are no files selected for viewing
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,63 @@ | ||
import * as vscode from "vscode"; | ||
import * as http from "http"; | ||
import * as https from "https"; | ||
|
||
let formatterRegistration: vscode.Disposable; | ||
export async function registerExternalFormatter( | ||
formatterEndpoint: string, | ||
languages: string[], | ||
httpMethod: string | ||
) { | ||
languages = languages || (await vscode.languages.getLanguages()); | ||
httpMethod = httpMethod || "POST"; | ||
if (formatterRegistration) { | ||
formatterRegistration.dispose(); | ||
} | ||
const url = new URL(formatterEndpoint); | ||
formatterRegistration = vscode.languages.registerDocumentFormattingEditProvider(languages, { | ||
provideDocumentFormattingEdits( | ||
doc: vscode.TextDocument | ||
): vscode.ProviderResult<vscode.TextEdit[]> { | ||
const payload = JSON.stringify({ | ||
file: doc.fileName, | ||
language: doc.languageId, | ||
snippet: doc.getText(), | ||
}); | ||
const options = { | ||
hostname: url.hostname, | ||
port: url.port, | ||
path: url.pathname, | ||
method: httpMethod, | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Content-Length": Buffer.byteLength(payload), | ||
}, | ||
}; | ||
const range = new vscode.Range( | ||
doc.lineAt(0).range.start, | ||
doc.lineAt(doc.lineCount - 1).range.end | ||
); | ||
return new Promise((accept, reject) => { | ||
const httpModule = url.protocol.startsWith("https") ? https : http; | ||
const req = httpModule.request(options, (res) => { | ||
let data = ""; | ||
res.on("data", (chunk) => { | ||
data += chunk; | ||
}); | ||
res.on("end", () => { | ||
accept([vscode.TextEdit.replace(range, data)]); | ||
}); | ||
res.on("error", (err) => { | ||
accept([]); // no edits | ||
vscode.window.showErrorMessage( | ||
`Failed to format document with custom formatter: ${err}`, | ||
"OK" | ||
); | ||
}); | ||
}); | ||
req.write(payload); | ||
req.end(); | ||
}); | ||
}, | ||
}); | ||
} |
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