Skip to content

Commit

Permalink
More types
Browse files Browse the repository at this point in the history
Related: #1225
  • Loading branch information
ssbarnea committed Apr 24, 2024
1 parent a85e6b0 commit 98106be
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 340 deletions.
4 changes: 3 additions & 1 deletion packages/ansible-language-server/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tasks:
desc: Update dependencies
cmds:
- npm install -g npm@latest
- # installs tools from .tool-versions
# - installs tools from .tool-versions
- asdf install
- "{{.VIRTUAL_ENV}}/bin/python3 -m pre_commit autoupdate"
- npm outdated
Expand Down Expand Up @@ -88,6 +88,7 @@ tasks:
dir: "{{ .TASKFILE_DIR }}"
desc: Run only ee tests
cmds:
- task: build
- >
source {{.VIRTUAL_ENV}}/bin/activate &&
bash -c 'npm run test-with-ee'
Expand All @@ -96,6 +97,7 @@ tasks:
desc: Run only non-ee tests
dir: "{{ .TASKFILE_DIR }}"
cmds:
- task: build
- >
source {{.VIRTUAL_ENV}}/bin/activate &&
bash -c 'npm run test-without-ee'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export async function doCompletion(
const inlineCollections = getDeclaredCollections(path);
const cursorAtEndOfLine = atEndOfLine(document, position);

let textEdit: TextEdit | undefined;
let textEdit: TextEdit;
const nodeRange = getNodeRange(node, document);
if (nodeRange) {
textEdit = {
Expand Down Expand Up @@ -347,47 +347,49 @@ export async function doCompletion(
const nodeRange = getNodeRange(node, document);

const option = keyOptions.get(keyNode.value as string);
const choices = [];
let defaultChoice = option.default;
if (option.type === "bool" && typeof option.default === "string") {
// the YAML parser does not recognize values such as 'Yes'/'no' as booleans
defaultChoice =
option.default.toLowerCase() === "yes" ? true : false;
}
if (option.choices) {
choices.push(...option.choices);
} else if (option.type === "bool") {
choices.push(true);
choices.push(false);
} else if (defaultChoice !== undefined) {
choices.push(defaultChoice);
}
return choices.map((choice, index) => {
let priority;
if (choice === defaultChoice) {
priority = priorityMap.defaultChoice;
} else {
priority = priorityMap.choice;
if (option) {
const choices = [];
let defaultChoice = option.default;
if (option.type === "bool" && typeof option.default === "string") {
// the YAML parser does not recognize values such as 'Yes'/'no' as booleans
defaultChoice =
option.default.toLowerCase() === "yes" ? true : false;
}
const insertValue = new String(choice).toString();
const completionItem: CompletionItem = {
label: insertValue,
detail: choice === defaultChoice ? "default" : undefined,
// using index preserves order from the specification
// except when overridden by the priority
sortText: priority.toString() + index.toString().padStart(3),
kind: CompletionItemKind.Value,
};
if (nodeRange) {
completionItem.textEdit = {
range: nodeRange,
newText: insertValue,
};
} else {
completionItem.insertText = insertValue;
if (option.choices) {
choices.push(...option.choices);
} else if (option.type === "bool") {
choices.push(true);
choices.push(false);
} else if (defaultChoice !== undefined) {
choices.push(defaultChoice);
}
return completionItem;
});
return choices.map((choice, index) => {
let priority;
if (choice === defaultChoice) {
priority = priorityMap.defaultChoice;
} else {
priority = priorityMap.choice;
}
const insertValue = new String(choice).toString();
const completionItem: CompletionItem = {
label: insertValue,
detail: choice === defaultChoice ? "default" : undefined,
// using index preserves order from the specification
// except when overridden by the priority
sortText: priority.toString() + index.toString().padStart(3),
kind: CompletionItemKind.Value,
};
if (nodeRange) {
completionItem.textEdit = {
range: nodeRange,
newText: insertValue,
};
} else {
completionItem.insertText = insertValue;
}
return completionItem;
});
}
}
}

Expand Down Expand Up @@ -616,7 +618,13 @@ function atEndOfLine(document: TextDocument, position: Position): boolean {
* @param nodeRange - range of the keyword in the document
* @returns boolean true if the key is the first element of the list, else false
*/
function firstElementOfList(document: TextDocument, nodeRange: Range): boolean {
function firstElementOfList(
document: TextDocument,
nodeRange: Range | undefined,
): boolean {
if (!nodeRange) {
return false;
}
const checkNodeRange = {
start: { line: nodeRange.start.line, character: 0 },
end: nodeRange.start,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export async function doValidate(
context?: WorkspaceFolderContext,
connection?: Connection,
): Promise<Map<string, Diagnostic[]>> {
let diagnosticsByFile;
let diagnosticsByFile: Map<string, Diagnostic[]> = new Map<
string,
Diagnostic[]
>();
if (quick || !context) {
// get validation from cache
diagnosticsByFile =
Expand Down Expand Up @@ -106,13 +109,8 @@ export function getYamlValidation(textDocument: TextDocument): Diagnostic[] {
yDoc.errors.forEach((error) => {
const [errStart, errEnd] = error.pos;
if (errStart) {
const start = textDocument.positionAt(
errStart !== undefined ? errStart : null,
);

const end = textDocument.positionAt(
errEnd !== undefined ? errEnd : null,
);
const start = textDocument.positionAt(errStart);
const end = textDocument.positionAt(errEnd);

const range = Range.create(start, end);

Expand All @@ -128,7 +126,15 @@ export function getYamlValidation(textDocument: TextDocument): Diagnostic[] {
severity = DiagnosticSeverity.Information;
break;
}
rangeTree.insert([error.linePos[0].line, error.linePos[1].line], {
let a: number = 0;
let b: number = 0;
if (error.linePos) {
a = error.linePos[0].line;
if (error.linePos[1]) {
b = error.linePos[1].line;
}
}
rangeTree.insert([a, b], {
message: error.message,
range: range || Range.create(0, 0, 0, 0),
severity: severity,
Expand Down
44 changes: 34 additions & 10 deletions packages/ansible-language-server/src/services/ansibleInventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@ import { WorkspaceFolderContext } from "./workspaceManager";
import { CommandRunner } from "../utils/commandRunner";
import { URI } from "vscode-uri";

type HostType = { host: string; priority: number };

type inventoryHostEntry = {
children: string[];
hosts: string[];
};

type inventoryType = Omit<
{
[name: string]: inventoryHostEntry;
},
"_meta"
>;

/* Example of minimal inventory object, anything else may be missing.
{
"_meta": {
"hostvars": {}
Expand Down Expand Up @@ -60,7 +75,7 @@ Example of more complex inventory.
export class AnsibleInventory {
private connection: Connection;
private context: WorkspaceFolderContext;
private _hostList: unknown[];
private _hostList: HostType[] = [];

constructor(connection: Connection, context: WorkspaceFolderContext) {
this.connection = connection;
Expand Down Expand Up @@ -92,9 +107,11 @@ export class AnsibleInventory {
defaultHostListPath,
);

let inventoryHostsObject = [];
let inventoryHostsObject = {} as inventoryType;
try {
inventoryHostsObject = JSON.parse(ansibleInventoryResult.stdout);
inventoryHostsObject = JSON.parse(
ansibleInventoryResult.stdout,
) as inventoryType;
} catch (error) {
this.connection.console.error(
`Exception in AnsibleInventory service: ${JSON.stringify(error)}`,
Expand All @@ -115,7 +132,7 @@ export class AnsibleInventory {
* @param hostObj - nested object of hosts
* @returns an array of object with host and priority as keys
*/
function parseInventoryHosts(hostObj: object): unknown[] {
function parseInventoryHosts(hostObj: inventoryType): HostType[] {
if (
!(
"all" in hostObj &&
Expand Down Expand Up @@ -145,9 +162,12 @@ function parseInventoryHosts(hostObj: object): unknown[] {
return { host: item, priority: 2 };
});

const allGroups = [...topLevelGroupsObjList, ...otherGroupsObjList];
const allGroups: HostType[] = [
...topLevelGroupsObjList,
...otherGroupsObjList,
];

let ungroupedHostsObjList = [];
let ungroupedHostsObjList: HostType[] = [];

if (
"ungrouped" in hostObj &&
Expand All @@ -157,13 +177,13 @@ function parseInventoryHosts(hostObj: object): unknown[] {
hostObj.ungrouped
) {
ungroupedHostsObjList = hostObj.ungrouped.hosts.map((item) => {
return { host: item, priority: 3 };
return { host: item, priority: 3 } as HostType;
});
}

// Add 'localhost' and 'all' to the inventory list
const localhostObj = { host: "localhost", priority: 5 };
const allHostObj = { host: "all", priority: 6 };
const localhostObj: HostType = { host: "localhost", priority: 5 };
const allHostObj: HostType = { host: "all", priority: 6 };

let allHosts = [localhostObj, allHostObj, ...ungroupedHostsObjList];

Expand All @@ -179,7 +199,11 @@ function parseInventoryHosts(hostObj: object): unknown[] {
return [...allGroups, ...allHosts];
}

function getChildGroups(groupList, hostObj, res = []) {
function getChildGroups(
groupList: string[],
hostObj: inventoryType,
res: string[] = [],
): string[] {
for (const host of groupList) {
if (hostObj[`${host}`].children) {
getChildGroups(hostObj[`${host}`].children, hostObj, res);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TextDocument } from "vscode-languageserver-textdocument";
import { Position } from "vscode-languageserver";
import { Hover, MarkupContent, Position } from "vscode-languageserver";
import { expect } from "chai";
import {
createTestWorkspaceManager,
Expand All @@ -12,6 +12,18 @@ import {
import { doHover } from "../../src/providers/hoverProvider";
import { WorkspaceFolderContext } from "../../src/services/workspaceManager";

function get_hover_value(hover: Hover | undefined | null): string {
if (hover) {
if (Array.isArray(hover)) {
return "";
} else {
if (Object.hasOwn(hover.contents as object, "value")) {
return (hover.contents as MarkupContent)["value"];
}
}
}
return "";
}
function testPlayKeywords(
context: WorkspaceFolderContext,
textDoc: TextDocument,
Expand Down Expand Up @@ -41,7 +53,11 @@ function testPlayKeywords(
position,
await context.docsLibrary,
);
expect(actualHover.contents["value"]).includes(doc);
if (actualHover) {
expect(get_hover_value(actualHover)).includes(doc);
} else {
expect(false);
}
});
});
}
Expand All @@ -65,7 +81,11 @@ function testTaskKeywords(
position,
await context.docsLibrary,
);
expect(actualHover.contents["value"]).includes(doc);
if (actualHover) {
expect(get_hover_value(actualHover)).includes(doc);
} else {
expect(false);
}
});
});
}
Expand All @@ -89,7 +109,11 @@ function testBlockKeywords(
position,
await context.docsLibrary,
);
expect(actualHover.contents["value"]).includes(doc);
if (actualHover) {
expect(get_hover_value(actualHover)).includes(doc);
} else {
expect(false);
}
});
});
}
Expand All @@ -113,7 +137,11 @@ function testRoleKeywords(
position,
await context.docsLibrary,
);
expect(actualHover.contents["value"]).includes(doc);
if (actualHover) {
expect(get_hover_value(actualHover)).includes(doc);
} else {
expect(false);
}
});
});
}
Expand Down Expand Up @@ -142,7 +170,7 @@ function testModuleNames(
position,
await context.docsLibrary,
);
expect(actualHover.contents["value"]).includes(doc);
expect(get_hover_value(actualHover)).includes(doc);
});
});
}
Expand Down Expand Up @@ -205,7 +233,7 @@ function testPlaybookAdjacentCollection(
position,
await context.docsLibrary,
);
expect(actualHover.contents["value"]).includes(doc);
expect(get_hover_value(actualHover)).includes(doc);
});
});
}
Expand Down Expand Up @@ -239,7 +267,7 @@ function testNonPlaybookAdjacentCollection(
expect(actualHover).to.be.null;
} else {
expect(
actualHover.contents["value"],
get_hover_value(actualHover),
`actual hover -> ${actualHover}`,
).includes(doc);
}
Expand Down
Loading

0 comments on commit 98106be

Please sign in to comment.