Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ Populate the `custom-format.formatters` setting.
// Create our custom formatters
"custom-format.formatters": [
{
// Whatever language id you need to format
"language": "javascript",
// Whatever language id you need to format - or a list of languages
"language": ["javascript", "typescript"],
// The command that will be run to format files with the language id specified above
"command": "node format.js $FILE" // $FILE is replaced with the path of the file to be formatted
},
{
"language": "typescript",
"command": "node format-ts.js"
"language": "html",
"command": "node format-html.js"
}
],

Expand Down
38 changes: 21 additions & 17 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { runCommand } from './run-command'

interface Formatter {
command: string
language: string
language: string | string[]
}

export function activate(context: ExtensionContext) {
Expand Down Expand Up @@ -42,27 +42,31 @@ export function activate(context: ExtensionContext) {
break
}

let disposable = languages.registerDocumentFormattingEditProvider(formatter.language, {
async provideDocumentFormattingEdits(document: TextDocument): Promise<TextEdit[]> {
const rawText = document.getText()
const filename = document.uri.fsPath
const workspaceDir = getCwd(filename)
const formattedText = await runCommand(rawText, formatter.command, filename, workspaceDir)
const languages = Array.isArray(formatter.language) ? formatter.language : [formatter.language]

const lastLineNumber = document.lineCount - 1
const lastLineChar = document.lineAt(lastLineNumber).text.length
for (let language of languages) {
let disposable = languages.registerDocumentFormattingEditProvider(language, {
async provideDocumentFormattingEdits(document: TextDocument): Promise<TextEdit[]> {
const rawText = document.getText()
const filename = document.uri.fsPath
const workspaceDir = getCwd(filename)
const formattedText = await runCommand(rawText, formatter.command, filename, workspaceDir)

const startPos = new Position(0, 0)
const endPos = new Position(lastLineNumber, lastLineChar)
const replaceRange = new Range(startPos, endPos)
const lastLineNumber = document.lineCount - 1
const lastLineChar = document.lineAt(lastLineNumber).text.length

return [TextEdit.replace(replaceRange, formattedText)]
},
})
const startPos = new Position(0, 0)
const endPos = new Position(lastLineNumber, lastLineChar)
const replaceRange = new Range(startPos, endPos)

listeningLanguages.push(formatter.language)
return [TextEdit.replace(replaceRange, formattedText)]
},
})

context.subscriptions.push(disposable)
listeningLanguages.push(language)

context.subscriptions.push(disposable)
}
}

log(`Formatting activated for the following languages: [${listeningLanguages.toLocaleString()}]`)
Expand Down