• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pascalre / vscode-yaml-sort / 3841862740

pending completion
3841862740

Pull #112

github

GitHub
Merge ea32ee516 into 89a7c4d0e
Pull Request #112: 110 add configuration for deepsourceio

130 of 163 branches covered (79.75%)

Branch coverage included in aggregate %.

106 of 106 new or added lines in 8 files covered. (100.0%)

379 of 405 relevant lines covered (93.58%)

13.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/src/controller.ts
1
import { TextEdit, Uri, window } from "vscode"
1✔
2
import { readFileSync, writeFileSync } from "fs"
1✔
3
import { JsYamlAdapter } from "./adapter/js-yaml-adapter"
1✔
4
import { Severity, VsCodeAdapter } from "./adapter/vs-code-adapter"
1✔
5
import { prependWhitespacesOnEachLine, removeLeadingLineBreakOfFirstElement } from "./lib"
1✔
6
import { Settings } from "./settings"
1✔
7
import { FileUtil } from "./util/file-util"
1✔
8
import { getDelimiters, splitYaml, validateTextRange, YamlUtil } from "./util/yaml-util"
1✔
9

10
const settings = new Settings()
1✔
11
const jsyamladapter = new JsYamlAdapter()
1✔
12
const vscodeadapter = new VsCodeAdapter()
1✔
13
const yamlutil = new YamlUtil()
1✔
14

15
export function sortYamlWrapper(customSort = 0): TextEdit[] {
1✔
16
  const activeEditor = window.activeTextEditor
4✔
17

18
  if (activeEditor) {
4!
19
    const textRange = VsCodeAdapter.getRange(activeEditor)
4✔
20
    let text = VsCodeAdapter.getText(activeEditor, textRange)
4✔
21

22
    try {
4✔
23
      validateTextRange(text)
4✔
24
      jsyamladapter.validateYaml(text)
2✔
25
    } catch (e) {
26
      if (e instanceof Error) {
2!
27
        vscodeadapter.showMessage(Severity.ERROR, e.message)
2✔
28
      }
29
      return [] as TextEdit[]
2✔
30
    }
31

32
    let numberOfLeadingSpaces = 0
2✔
33

34
    let delimiters = getDelimiters(text, activeEditor.selection.isEmpty, settings.getUseLeadingDashes())
2✔
35
    // remove yaml metadata tags
36
    const matchMetadata = /^%.*\n/gm
2✔
37
    // set metadata tags, if there is no metadata tag it should be an emtpy array
38
    if (matchMetadata.test(text)) {
2!
39
      delimiters.shift()
×
40
      delimiters = removeLeadingLineBreakOfFirstElement(delimiters)
×
41
    }
42
    text = text.replace(matchMetadata, "")
2✔
43
    text = text.replace(/^\n/, "")
2✔
44

45
    // sort yaml
46
    let newText = ""
2✔
47
    splitYaml(text).forEach((unsortedYaml) => {
2✔
48
      let sortedYaml = yamlutil.sortYaml(unsortedYaml, customSort)
2✔
49
      if (sortedYaml) {
2!
50
        if (!activeEditor.selection.isEmpty) {
2✔
51
          // get number of leading whitespaces, these whitespaces will be used for indentation
52
          numberOfLeadingSpaces = YamlUtil.getNumberOfLeadingSpaces(unsortedYaml)
1✔
53
          sortedYaml = prependWhitespacesOnEachLine(sortedYaml, numberOfLeadingSpaces)
1✔
54
        }
55
        newText += delimiters.shift() + sortedYaml
2✔
56
      } else {
57
        return [] as TextEdit[]
×
58
      }
59
    })
60
    if (activeEditor.selection.isEmpty && settings.getUseLeadingDashes()) {
2✔
61
      newText = `---\n${newText}`
1✔
62
    }
63

64
    const edits = VsCodeAdapter.getEdits(activeEditor, newText)
2✔
65
    vscodeadapter.showMessage(Severity.INFO, "Keys resorted successfully")
2✔
66
    VsCodeAdapter.applyEdits([edits])
2✔
67
    return [edits]
2✔
68
  }
69
  return [] as TextEdit[]
×
70
}
71

72
export function validateYamlWrapper(): boolean {
1✔
73
  const activeEditor = window.activeTextEditor
2✔
74

75
  if (activeEditor) {
2!
76
    const text = VsCodeAdapter.getActiveDocument(activeEditor)
2✔
77
    try {
2✔
78
      jsyamladapter.validateYaml(text)
2✔
79
      vscodeadapter.showMessage(Severity.INFO, "YAML is valid.")
2✔
80
      return true
2✔
81
    } catch (e) {
82
      if (e instanceof Error) {
×
83
        vscodeadapter.showMessage(Severity.INFO, `YAML is invalid. ${e.message}`)
×
84
      }
85
    }
86
  }
87
  return false
×
88
}
89

90
export function formatYamlWrapper(): TextEdit[] {
1✔
91
  const activeEditor = window.activeTextEditor
2✔
92

93
  if (activeEditor) {
2!
94
    let doc = activeEditor.document.getText()
2✔
95
    let delimiters = getDelimiters(doc, true, settings.getUseLeadingDashes())
2✔
96
    // remove yaml metadata tags
97
    const matchMetadata = /^%.*\n/gm
2✔
98
    // set metadata tags, if there is no metadata tag it should be an emtpy array
99
    if (matchMetadata.test(doc)) {
2!
100
      delimiters.shift()
×
101
      delimiters = removeLeadingLineBreakOfFirstElement(delimiters)
×
102
    }
103
    doc = doc.replace(matchMetadata, "")
2✔
104
    doc = doc.replace(/^\n/, "")
2✔
105

106
    let formattedYaml
107
    const yamls = splitYaml(doc)
2✔
108
    let newText = ""
2✔
109
    for (const unformattedYaml of yamls) {
2✔
110
      formattedYaml = yamlutil.formatYaml(unformattedYaml, false)
3✔
111
      if (formattedYaml) {
3!
112
        newText += delimiters.shift() + formattedYaml
3✔
113
      } else {
114
        return []
×
115
      }
116
    }
117
    if (settings.getUseLeadingDashes()) {
2!
118
      newText = `---\n${newText}`
2✔
119
    }
120
    const edits = VsCodeAdapter.getEdits(activeEditor, newText)
2✔
121
    VsCodeAdapter.applyEdits([edits])
2✔
122
    return [edits]
2✔
123
  }
124
  return []
×
125
}
126

127
/**
128
 * Sorts all yaml files in a directory
129
 * @param {Uri} uri Base URI
130
 */
131
export function sortYamlFiles(uri: Uri): boolean {
1✔
132
  const files = new FileUtil().getFilesWithExtensions(uri.fsPath)
2✔
133
  files.forEach((file: string) => {
2✔
134
    const yaml = readFileSync(file, 'utf-8').toString()
3✔
135
    const sortedYaml = yamlutil.sortYaml(yaml, 0)
3✔
136

137
    if (sortedYaml) {
3✔
138
      try {
2✔
139
        writeFileSync(file, sortedYaml)
2✔
140
      } catch (e) {
141
        /* istanbul ignore next */
142
        vscodeadapter.showMessage(Severity.ERROR, `File ${file} could not be sorted`)
143
      }
144
    } else {
145
      vscodeadapter.showMessage(Severity.ERROR, `File ${file} could not be sorted`)
1✔
146
    }
147
  })
148
  return true
2✔
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc