Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-indent HCL with Ctrl+Shift+F #69

Merged
merged 1 commit into from
Mar 9, 2022
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

### Added

- Auto-indentation via <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>F</kbd> [#69](https://github.com/avenga/couper-vscode/pull/69)
- `log_level` attribute for `settings` block [#62](https://github.com/avenga/couper-vscode/pull/62)
- `disable_private_caching` attribute for `jwt` block [#65](https://github.com/avenga/couper-vscode/pull/65)
- `beta_scope_map` attribute for `jwt` block [#66](https://github.com/avenga/couper-vscode/pull/66)
- `backend_request` and `backend_response` variables [#64](https://github.com/avenga/couper-vscode/pull/64)


### Fixed

- type of `custom_log_fields` [#62](https://github.com/avenga/couper-vscode/pull/62)
Expand Down
3 changes: 2 additions & 1 deletion src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Object.defineProperty(exports, "__esModule", { value: true })

const Completion = require("./completion")
const Definition = require("./definition")
const Formatter = require("./formatter")

exports.activate = (context) => {
context.subscriptions.concat(Completion.providers, Definition.providers)
context.subscriptions.concat(Completion.providers, Definition.providers, Formatter.providers)
console.info("Extension loaded: Couper Configuration")
}
65 changes: 65 additions & 0 deletions src/formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"use strict"

const vscode = require('vscode')

const selector = { language: 'couper' }

const HEREDOC_REGEX = /^\s*[\w-]+\s*=\s*<<(-?)([\w-]+)\s*$/
const LBRACE_REGEX = /[{(\[]\s*$/
const RBRACE_REGEX = /^\s*[})\]]\s*$/

const providers = []

const formattingProvider = vscode.languages.registerDocumentFormattingEditProvider(selector, {
provideDocumentFormattingEdits(document, options) {
const padding = options.insertSpaces ? " " : "\t"
const indentFactor = options.insertSpaces ? options.tabSize : 1

let edits = []
let indentDepth = 0
let hereDocDelimiter
let isHereDoc = false
let isIndentedHereDoc = false
let hereDocEndRegex

for (let i = 0; i < document.lineCount; i++) {
const line = document.lineAt(i)
if (!isHereDoc && RBRACE_REGEX.test(line.text)) {
indentDepth--
} else if (isHereDoc) {
const foundHereDocEnd = hereDocEndRegex.test(line.text)
if (foundHereDocEnd && isIndentedHereDoc) {
indentDepth--
}
isHereDoc = !foundHereDocEnd
}

if (!isHereDoc || isIndentedHereDoc) {
const text = line.text.substring(line.firstNonWhitespaceCharacterIndex)
const indentation = "".padStart(indentDepth * indentFactor, padding)
edits.push(vscode.TextEdit.replace(line.range, indentation + text))
}

if (!isHereDoc && LBRACE_REGEX.test(line.text)) {
indentDepth++
continue
}

const matches = line.text.match(HEREDOC_REGEX)
if (matches !== null) {
isIndentedHereDoc = matches[1] === "-"
hereDocDelimiter = matches[2]
isHereDoc = true
hereDocEndRegex = new RegExp('^\\s*' + hereDocDelimiter + '\\s*$')
if (isIndentedHereDoc) {
indentDepth++
}
}
}
return edits
}
})

providers.push(formattingProvider)

exports.providers = providers
2 changes: 1 addition & 1 deletion syntaxes/hcl.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
"patterns" : [
{
"comment" : "Heredoc string",
"begin": "<<-?(\\w+)",
"begin": "<<-?([\\w-]+)",
"beginCaptures": {
"0": { "name": "entity.name.tag.hcl" }
},
Expand Down