Skip to content
Open
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: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"label": "watch typescript",
"type": "shell",
"command": "yarn run watch",
"command": "npm run watch",
"presentation": {
"reveal": "never"
},
Expand Down
4 changes: 3 additions & 1 deletion l10n/bundle.l10n.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
"flowStyleMapForbidden": "Flow-Stil-Mapping ist verboten",
"flowStyleSeqForbidden": "Flow-Stil-Sequenz ist verboten",
"unUsedAnchor": "Nicht verwendeter Anker \"{0}\"",
"unUsedAlias": "Nicht aufgelöstes Alias \"{0}\""
"unUsedAlias": "Nicht aufgelöstes Alias \"{0}\"",
"convertToFoldedBlockString": "Konvertieren Sie die Zeichenfolge in eine gefaltete Blockzeichenfolge",
"convertToLiteralBlockString": "Konvertieren Sie die Zeichenfolge in eine wörtliche Blockzeichenfolge"
}
4 changes: 3 additions & 1 deletion l10n/bundle.l10n.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
"flowStyleMapForbidden": "Le mappage de style de flux est interdit",
"flowStyleSeqForbidden": "La séquence de style Flow est interdite",
"unUsedAnchor": "Ancre inutilisée '{0}'",
"unUsedAlias": "Alias ​​non résolu '{0}'"
"unUsedAlias": "Alias non résolu '{0}'",
"convertToFoldedBlockString": "Convertir la chaîne en style de bloc pliée",
"convertToLiteralBlockString": "Convertir la chaîne en style de bloc littérale"
}
4 changes: 3 additions & 1 deletion l10n/bundle.l10n.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
"flowStyleMapForbidden": "フロースタイルのマッピングは禁止されています",
"flowStyleSeqForbidden": "フロースタイルのシーケンスは禁止されています",
"unUsedAnchor": "未使用のアンカー \"{0}\"",
"unUsedAlias": "未解決のエイリアス \"{0}\""
"unUsedAlias": "未解決のエイリアス \"{0}\"",
"convertToFoldedBlockString": "文字列を折り畳みブロック文字列に変換する",
"convertToLiteralBlockString": "文字列をリテラルブロック文字列に変換する"
}
4 changes: 3 additions & 1 deletion l10n/bundle.l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
"flowStyleMapForbidden": "Flow style mapping is forbidden",
"flowStyleSeqForbidden": "Flow style sequence is forbidden",
"unUsedAnchor": "Unused anchor \"{0}\"",
"unUsedAlias": "Unresolved alias \"{0}\""
"unUsedAlias": "Unresolved alias \"{0}\"",
"convertToFoldedBlockString": "Convert string to folded block string",
"convertToLiteralBlockString": "Convert string to literal block string"
}
4 changes: 3 additions & 1 deletion l10n/bundle.l10n.ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
"flowStyleMapForbidden": "Flow 스타일 맵 사용이 금지됨",
"flowStyleSeqForbidden": "Flow 스타일 시퀀스 사용이 금지됨",
"unUsedAnchor": "사용되지 않은 앵커 \"{0}\"",
"unUsedAlias": "해결되지 않은 별칭 \"{0}\""
"unUsedAlias": "해결되지 않은 별칭 \"{0}\"",
"convertToFoldedBlockString": "문자열을 접힌 블록 문자열로 변환합니다.",
"convertToLiteralBlockString": "문자열을 리터럴 블록 문자열로 변환합니다."
}
4 changes: 3 additions & 1 deletion l10n/bundle.l10n.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
"flowStyleMapForbidden": "禁止使用 flow 样式的映射",
"flowStyleSeqForbidden": "禁止使用 flow 样式的序列",
"unUsedAnchor": "未使用的锚点 \"{0}\"",
"unUsedAlias": "未解析的别名 \"{0}\""
"unUsedAlias": "未解析的别名 \"{0}\"",
"convertToFoldedBlockString": "将字符串转换为折叠块字符串",
"convertToLiteralBlockString": "将字符串转换为文字块字符串"
}
4 changes: 3 additions & 1 deletion l10n/bundle.l10n.zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
"flowStyleMapForbidden": "禁止使用 Flow 風格的對應",
"flowStyleSeqForbidden": "禁止使用 Flow 風格的序列",
"unUsedAnchor": "未使用的錨點 \"{0}\"",
"unUsedAlias": "未解析的別名 \"{0}\""
"unUsedAlias": "未解析的別名 \"{0}\"",
"convertToFoldedBlockString": "將字串轉換為折疊塊字串",
"convertToLiteralBlockString": "將字串轉換為文字區塊字串"
}
73 changes: 60 additions & 13 deletions src/languageservice/services/yamlCodeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as l10n from '@vscode/l10n';
import * as _ from 'lodash';
import * as path from 'path';
import { ErrorCode } from 'vscode-json-languageservice';
import { ClientCapabilities, CodeActionParams } from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import {
CodeAction,
Expand All @@ -14,22 +19,18 @@ import {
TextEdit,
WorkspaceEdit,
} from 'vscode-languageserver-types';
import { ClientCapabilities, CodeActionParams } from 'vscode-languageserver-protocol';
import { CST, isMap, isScalar, isSeq, Scalar, visit, YAMLMap } from 'yaml';
import { SourceToken } from 'yaml/dist/parse/cst';
import { YamlCommands } from '../../commands';
import * as path from 'path';
import { TextBuffer } from '../utils/textBuffer';
import { LanguageSettings } from '../yamlLanguageService';
import { ASTNode } from '../jsonASTTypes';
import { YAML_SOURCE } from '../parser/jsonParser07';
import { getFirstNonWhitespaceCharacterAfterOffset } from '../utils/strings';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { CST, isMap, isSeq, YAMLMap } from 'yaml';
import { yamlDocumentsCache } from '../parser/yaml-documents';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { BlockStringRewriter } from '../utils/block-string-rewriter';
import { FlowStyleRewriter } from '../utils/flow-style-rewriter';
import { ASTNode } from '../jsonASTTypes';
import * as _ from 'lodash';
import { SourceToken } from 'yaml/dist/parse/cst';
import { ErrorCode } from 'vscode-json-languageservice';
import * as l10n from '@vscode/l10n';
import { getFirstNonWhitespaceCharacterAfterOffset } from '../utils/strings';
import { TextBuffer } from '../utils/textBuffer';
import { LanguageSettings } from '../yamlLanguageService';

interface YamlDiagnosticData {
schemaUri: string[];
Expand All @@ -38,11 +39,13 @@ interface YamlDiagnosticData {
}
export class YamlCodeActions {
private indentation = ' ';
private lineWidth = 80;

constructor(private readonly clientCapabilities: ClientCapabilities) {}

configure(settings: LanguageSettings): void {
configure(settings: LanguageSettings, printWidth: number): void {
this.indentation = settings.indentation;
this.lineWidth = printWidth;
}

getCodeAction(document: TextDocument, params: CodeActionParams): CodeAction[] | undefined {
Expand All @@ -57,6 +60,7 @@ export class YamlCodeActions {
result.push(...this.getTabToSpaceConverting(params.context.diagnostics, document));
result.push(...this.getUnusedAnchorsDelete(params.context.diagnostics, document));
result.push(...this.getConvertToBlockStyleActions(params.context.diagnostics, document));
result.push(...this.getConvertStringToBlockStyleActions(params.context.diagnostics, document));
result.push(...this.getKeyOrderActions(params.context.diagnostics, document));
result.push(...this.getQuickFixForPropertyOrValueMismatch(params.context.diagnostics, document));

Expand Down Expand Up @@ -243,6 +247,49 @@ export class YamlCodeActions {
return results;
}

private getConvertStringToBlockStyleActions(diagnostics: Diagnostic[], document: TextDocument): CodeAction[] {
const yamlDocument = yamlDocumentsCache.getYamlDocument(document);

const results: CodeAction[] = [];
for (const singleYamlDocument of yamlDocument.documents) {
const matchingNodes: Scalar<string>[] = [];
visit(singleYamlDocument.internalDocument, (key, node) => {
if (isScalar(node)) {
if (node.type === 'QUOTE_DOUBLE' || node.type === 'QUOTE_SINGLE') {
if (typeof node.value === 'string' && (node.value.indexOf('\n') >= 0 || node.value.length > this.lineWidth)) {
matchingNodes.push(<Scalar<string>>node);
}
}
}
});
for (const node of matchingNodes) {
const range = Range.create(document.positionAt(node.range[0]), document.positionAt(node.range[2]));
const rewriter = new BlockStringRewriter(this.indentation, this.lineWidth);
const foldedBlockScalar = rewriter.writeFoldedBlockScalar(node);
if (foldedBlockScalar !== null) {
results.push(
CodeAction.create(
l10n.t('convertToFoldedBlockString'),
createWorkspaceEdit(document.uri, [TextEdit.replace(range, foldedBlockScalar)]),
CodeActionKind.Refactor
)
);
}
const literalBlockScalar = rewriter.writeLiteralBlockScalar(node);
if (literalBlockScalar !== null) {
results.push(
CodeAction.create(
l10n.t('convertToLiteralBlockString'),
createWorkspaceEdit(document.uri, [TextEdit.replace(range, literalBlockScalar)]),
CodeActionKind.Refactor
)
);
}
}
}
return results;
}

private getKeyOrderActions(diagnostics: Diagnostic[], document: TextDocument): CodeAction[] {
const results: CodeAction[] = [];
for (const diagnostic of diagnostics) {
Expand Down
Loading