Run prettier on language service
This commit is contained in:
@@ -2,11 +2,11 @@ import {
|
||||
TemplateToken,
|
||||
MAPPING_TYPE,
|
||||
SEQUENCE_TYPE,
|
||||
NULL_TYPE,
|
||||
NULL_TYPE
|
||||
} from "@github/actions-workflow-parser/templates/tokens/index";
|
||||
import { MappingToken } from "@github/actions-workflow-parser/templates/tokens/mapping-token";
|
||||
import { SequenceToken } from "@github/actions-workflow-parser/templates/tokens/sequence-token";
|
||||
import { Position } from "vscode-languageserver-textdocument";
|
||||
import {MappingToken} from "@github/actions-workflow-parser/templates/tokens/mapping-token";
|
||||
import {SequenceToken} from "@github/actions-workflow-parser/templates/tokens/sequence-token";
|
||||
import {Position} from "vscode-languageserver-textdocument";
|
||||
|
||||
export function findInnerToken(pos: Position, root?: TemplateToken) {
|
||||
const [innerToken, _] = findInnerTokenAndParent(pos, root);
|
||||
@@ -41,7 +41,7 @@ export function findInnerTokenAndParent(
|
||||
const mappingToken = token as MappingToken;
|
||||
parent = mappingToken;
|
||||
for (let i = 0; i < mappingToken.count; i++) {
|
||||
const { key, value } = mappingToken.get(i);
|
||||
const {key, value} = mappingToken.get(i);
|
||||
|
||||
// Null tokens don't have a position, we can only use the line information
|
||||
if (nullNodeOnLine(pos, key, value)) {
|
||||
@@ -84,21 +84,14 @@ function posInToken(pos: Position, token: TemplateToken): boolean {
|
||||
|
||||
// Position is within the token lines. Check character/column if pos line matches
|
||||
// start or end
|
||||
if (
|
||||
(r.start[0] === tokenLine && tokenChar < r.start[1]) ||
|
||||
(r.end[0] === tokenLine && tokenChar > r.end[1])
|
||||
) {
|
||||
if ((r.start[0] === tokenLine && tokenChar < r.start[1]) || (r.end[0] === tokenLine && tokenChar > r.end[1])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function nullNodeOnLine(
|
||||
pos: Position,
|
||||
key: TemplateToken,
|
||||
value: TemplateToken
|
||||
): boolean {
|
||||
function nullNodeOnLine(pos: Position, key: TemplateToken, value: TemplateToken): boolean {
|
||||
if (value.templateTokenType !== NULL_TYPE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
import { Position, TextDocument } from "vscode-languageserver-textdocument";
|
||||
import {Position, TextDocument} from "vscode-languageserver-textdocument";
|
||||
|
||||
const DUMMY_KEY = "dummy";
|
||||
|
||||
// Transform a document to work around YAML parsing issues
|
||||
// Based on `_transform` in https://github.com/cschleiden/github-actions-parser/blob/main/src/lib/parser/complete.ts#L311
|
||||
export function transform(
|
||||
doc: TextDocument,
|
||||
pos: Position
|
||||
): [TextDocument, Position] {
|
||||
export function transform(doc: TextDocument, pos: Position): [TextDocument, Position] {
|
||||
const input = doc.getText();
|
||||
let offset = doc.offsetAt(pos);
|
||||
// TODO: Optimize this...
|
||||
@@ -15,9 +12,8 @@ export function transform(
|
||||
const lineNo = input
|
||||
.substring(0, offset)
|
||||
.split("")
|
||||
.filter((x) => x === "\n").length;
|
||||
const linePos =
|
||||
offset - lines.slice(0, lineNo).reduce((p, l) => p + l.length + 1, 0);
|
||||
.filter(x => x === "\n").length;
|
||||
const linePos = offset - lines.slice(0, lineNo).reduce((p, l) => p + l.length + 1, 0);
|
||||
const line = lines[lineNo];
|
||||
|
||||
let partialInput = line.trim();
|
||||
@@ -37,11 +33,7 @@ export function transform(
|
||||
}
|
||||
|
||||
lines[lineNo] =
|
||||
line.substring(0, linePos) +
|
||||
spacer +
|
||||
DUMMY_KEY +
|
||||
(trimmedLine === "-" ? "" : ":") +
|
||||
line.substring(linePos);
|
||||
line.substring(0, linePos) + spacer + DUMMY_KEY + (trimmedLine === "-" ? "" : ":") + line.substring(linePos);
|
||||
|
||||
// Adjust pos by one to prevent a sequence node being marked as active
|
||||
offset++;
|
||||
@@ -51,22 +43,13 @@ export function transform(
|
||||
}
|
||||
|
||||
if (trimmedLine.startsWith("-")) {
|
||||
partialInput = trimmedLine
|
||||
.substring(trimmedLine.indexOf("-") + 1)
|
||||
.trim();
|
||||
partialInput = trimmedLine.substring(trimmedLine.indexOf("-") + 1).trim();
|
||||
}
|
||||
} else {
|
||||
partialInput = (
|
||||
offset > colon ? line.substring(colon + 1) : line.substring(0, colon)
|
||||
).trim();
|
||||
partialInput = (offset > colon ? line.substring(colon + 1) : line.substring(0, colon)).trim();
|
||||
offset = offset - 1;
|
||||
}
|
||||
}
|
||||
const newDoc = TextDocument.create(
|
||||
doc.uri,
|
||||
doc.languageId,
|
||||
doc.version,
|
||||
lines.join("\n")
|
||||
);
|
||||
const newDoc = TextDocument.create(doc.uri, doc.languageId, doc.version, lines.join("\n"));
|
||||
return [newDoc, newDoc.positionAt(offset)];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user