Skip to content

Commit

Permalink
Merge d3493a9 into e7701f4
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalre committed Dec 6, 2022
2 parents e7701f4 + d3493a9 commit 1f731c9
Show file tree
Hide file tree
Showing 28 changed files with 1,718 additions and 1,277 deletions.
6 changes: 0 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// A launch configuration that compiles the extension and then opens it inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [{
Expand All @@ -15,8 +11,6 @@
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
//"preLaunchTask": "npm run"
//"preLaunchTask": "npm: watch"
},
{
"name": "Extension Tests",
Expand Down
13 changes: 9 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ The following commands support you at developing good code:
* `npm run test`: Run the tests
* `npm run lint`: Run the linter

Run the following commands to run SonarScanner:
* `docker pull sonarqube`: Pull sonarqube container.
* `docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:latest`: Start sonarqube container. This can take some time.
### SonarQube
* `docker-compose up -d`: Start the sonarqube container. This will take some time.
* `docker-compose stop`: Stop the container
* `docker-compose down`: Remove the container

### Commit Changes
SonarQube will be reachable on `http://localhost:9000`.

* `npm run sonar`: Start sonarscanner and upload result to local container

### Commit changes
Refer to [this blogpost](https://cbea.ms/git-commit/#end) by cbeams when committing changes. Issue numbers can be added in braces to the end of the commit message.

It is also required to test your changes. Detailled information about test coverage will be published to [coveralls](https://coveralls.io/github/pascalre/vscode-yaml-sort).
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
sonarqube:
platform: linux/amd64
container_name: sonarqube
image: sonarqube:latest
ports:
- "9000:9000"
- "9092:9092"
Binary file modified images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
"coverage": "rm -rf .nyc_output && rm -rf coverage && nyc npm run test",
"lint": "eslint . --ext .ts",
"pretest": "npm run compile",
"sonar": "node sonar-project.js",
"test": "node ./out/test/runTest.js",
"vscode:prepublish": "npm run compile",
"watch": "tsc -watch -p ./"
Expand Down
4 changes: 3 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:base"
"config:base",
"schedule:monthly",
"group:recommended"
]
}
20 changes: 20 additions & 0 deletions sonar-project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable no-undef */
// eslint-disable-next-line @typescript-eslint/no-var-requires
const scanner = require('sonarqube-scanner');
scanner(
{
serverUrl: 'http://localhost:9000',
options : {
'sonar.projectName': 'YAML Sort',
'sonar.projectKey': 'vscode-yaml-sort',
'sonar.sources': 'src',
'sonar.language': 'typescript',
'sonar.test.inclusions': 'src/**/*.test.ts',
'sonar.exclusions': 'node_modules',
'sonar.binaries': 'out',
'sonar.tests': 'src/test',
'sonar.typescript.lcov.reportPaths': 'coverage/lcov.info',
}
},
() => process.exit()
);
12 changes: 0 additions & 12 deletions sonar-project.properties

This file was deleted.

67 changes: 67 additions & 0 deletions src/adapter/js-yaml-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Settings } from "../settings"
import * as jsyaml from "js-yaml"
import { SortUtil } from "../util/sort-util"
import { removeTrailingCharacters, splitYaml } from "../util/yaml-util"

export class JsYamlAdapter {
settings: Settings

constructor(settings = new Settings()) {
this.settings = settings
}

getLoadOptions() {
return { schema: this.settings.getSchema() }
}

load(text: string) {
return jsyaml.load(text, this.getLoadOptions())
}

/**
* Validates a yaml document
* @param text Yaml document
* @returns true if yaml is valid
*/
validateYaml(text: string): boolean {
splitYaml(text).forEach((yaml) => {
jsyaml.load(yaml, { schema: this.settings.getSchema() })
})
return true
}


/**
* Dumps a yaml with the user specific settings.
* @param {string} text Yaml document which should be dumped.
* @param {boolean} sortKeys If set to true, the function will sort the keys in the document. Defaults to true.
* @returns {string} Clean yaml document.
*/
dumpYaml(text: string, sortKeys: boolean, custom: number, settings: Settings): string {

if (Object.keys(text).length === 0) {
return ""
}

const sort = new SortUtil(settings, custom)

let yaml = jsyaml.dump(text, {
indent: settings.getIndent(),
forceQuotes: settings.getForceQuotes(),
lineWidth: settings.getLineWidth(),
noArrayIndent: settings.getNoArrayIndent(),
noCompatMode: settings.getNoCompatMode(),
quotingType: settings.getQuotingType(),
schema: settings.getSchema(),
sortKeys: (!(custom > 0 && settings.getUseCustomSortRecursively()) ? sortKeys : (a: string, b: string) => {
return sort.customSort(a, b)
})
})

// this is neccesary to avoid linebreaks in a selection sort
yaml = removeTrailingCharacters(yaml, 1)

return yaml
}

}
98 changes: 98 additions & 0 deletions src/adapter/vs-code-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import * as vscode from "vscode"
import { Settings } from "../settings"

export enum Severity {
INFO, ERROR
}

export class VsCodeAdapter {
section = "vscode-yaml-sort"
settings: Settings

constructor(settings = new Settings()) {
this.settings = settings
}

getProperty(property: string) {
return vscode.workspace.getConfiguration().get(`${this.section}.${property}`)
}

getActiveDocument(textEditor: vscode.TextEditor) {
return textEditor.document.getText()
}

getFullDocumentRange(textEditor: vscode.TextEditor) {
return new vscode.Range(
new vscode.Position(0, 0),
new vscode.Position(textEditor.document.lineCount + 1, 0))
}

getSelectedRange(textEditor: vscode.TextEditor) {
let endLine = textEditor.selection.end.line
// if selection ends on the first character on a new line ignore this line
if (textEditor.selection.end.character === 0) {
endLine--
}

// ensure selection covers whole start and end line
return new vscode.Selection(
textEditor.selection.start.line, 0,
endLine, textEditor.document.lineAt(endLine).range.end.character)
}

getRange(textEditor: vscode.TextEditor) {
if (textEditor.selection.isEmpty) {
return this.getFullDocumentRange(textEditor)
} else {
return this.getSelectedRange(textEditor)
}
}

getText(textEditor: vscode.TextEditor, range: vscode.Range) {
return textEditor.document.getText(range)
}

getEdits(textEditor: vscode.TextEditor, text: string) {
const range = this.getRange(textEditor)
return vscode.TextEdit.replace(range, text)
}

/**
* Applys edits to a text editor
* @param activeEditor Editor to apply the changes
* @param edits Changes to apply
*/
applyEdits(edit: [vscode.TextEdit]) {
if (vscode.window.activeTextEditor) {
const workspaceEdit = new vscode.WorkspaceEdit()
workspaceEdit.set(vscode.window.activeTextEditor.document.uri, edit)
vscode.workspace.applyEdit(workspaceEdit)
}
}

// have a function that adds/removes the formatter based
// on a configuration setting
registerFormatter(formatter: vscode.DocumentFormattingEditProvider) {
let registration: vscode.Disposable | undefined
const useAsFormatter = this.settings.getUseAsFormatter()
if (useAsFormatter && !registration) {
vscode.languages.registerDocumentFormattingEditProvider('yaml', formatter)
} else if (!useAsFormatter && registration) {
registration.dispose()
}
}

showMessage(severity: Severity, message: string) {
switch(severity) {
case Severity.ERROR :
vscode.window.showErrorMessage(message)
break
case Severity.INFO :
if (this.settings.getNotifySuccess()) {
vscode.window.showInformationMessage(message)
}
break
}
}
}

Loading

0 comments on commit 1f731c9

Please sign in to comment.