Skip to content

Commit

Permalink
Auto formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyTy committed Jun 21, 2024
1 parent bc002f1 commit 8676064
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
27 changes: 27 additions & 0 deletions extension/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,33 @@ exports.activate = function (context) {
{ label: 'Hexa' }
)

// Formatting
vscode.languages.registerDocumentFormattingEditProvider('hexa', {
provideDocumentFormattingEdits(document/*: vscode.TextDocument*/, options, token)/*: vscode.TextEdit[]*/ {
const fullText = document.getText()
const fullFile = new vscode.Range(document.lineAt(0).range.start, document.lineAt(document.lineCount - 1).range.end)

return new Promise((resolve, reject) => {
const commands = [linter.wholeDocumentFormatting(fullText)]

linter.request(commands,
onJSON => {
const code = onJSON[0][0].content
if (code && code.length > 0) {
const edits = [vscode.TextEdit.replace(fullFile, code)]
resolve(edits)
} else {
reject()
}
},
onError => {
reject()
}
)
})
}
})

/** @implements {HoverProvider} */
class HexaHoverProvider {
/**
Expand Down
7 changes: 7 additions & 0 deletions extension/hexa-linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ class HexaLinter {
}
}

wholeDocumentFormatting(content) {
return {
kind: 'WholeDocumentFormatting',
payload: content
}
}

request(commands, onJSON, onError) {
const req = http.request(options, res => {
const chunks = []
Expand Down

0 comments on commit 8676064

Please sign in to comment.