Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.
Merged
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ rules/tags added to `warn_list`
(see [Ansible Lint Documentation](https://ansible-lint.readthedocs.io/en/latest/configuring.html))
are shown as warnings instead.

> If you also install `yamllint`, `ansible-lint` will detect it and incorporate
> into the linting process. Any findings reported by `yamllint` will be exposed
> in VSCode as errors/warnings.

### Smart autocompletion
![Autocompletion](images/smart-completions.gif)

Expand Down Expand Up @@ -92,7 +96,8 @@ holding `ctrl`/`cmd`.
## Requirements
- [Ansible 2.9+](https://docs.ansible.com/ansible/latest/index.html)
- [Ansible Lint](https://ansible-lint.readthedocs.io/en/latest/) (required,
unless you disable linter support; install without `yamllint`)
unless you disable linter support)
- [yamllint](https://yamllint.readthedocs.io/en/stable/) (optional)

For Windows users, this extension works perfectly well with extensions such as
`Remote - WSL` and `Remote - Containers`.
Expand Down
10 changes: 6 additions & 4 deletions src/providers/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
TextEdit,
} from 'vscode-languageserver';
import { Position, TextDocument } from 'vscode-languageserver-textdocument';
import { parseAllDocuments } from 'yaml';
import { Node, Pair, Scalar, YAMLMap } from 'yaml/types';
import { IOption } from '../interfaces/module';
import { WorkspaceFolderContext } from '../services/workspaceManager';
Expand All @@ -24,11 +23,13 @@ import {
findProvidedModule,
getDeclaredCollections,
getPathAt,
getOrigRange,
getYamlMapKeys,
isBlockParam,
isPlayParam,
isRoleParam,
isTaskParam,
parseAllDocuments,
} from '../utils/yaml';

const priorityMap = {
Expand Down Expand Up @@ -293,9 +294,10 @@ function getKeywordCompletion(
* the node has range information and is a string scalar.
*/
function getNodeRange(node: Node, document: TextDocument): Range | undefined {
if (node.range && node instanceof Scalar && typeof node.value === 'string') {
const start = node.range[0];
let end = node.range[1];
const range = getOrigRange(node);
if (range && node instanceof Scalar && typeof node.value === 'string') {
const start = range[0];
let end = range[1];
// compensate for `_:`
if (node.value.includes('_:')) {
end -= 2;
Expand Down
14 changes: 10 additions & 4 deletions src/providers/definitionProvider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { DefinitionLink, Range } from 'vscode-languageserver';
import { Position, TextDocument } from 'vscode-languageserver-textdocument';
import { parseAllDocuments } from 'yaml';
import { Scalar } from 'yaml/types';
import { DocsLibrary } from '../services/docsLibrary';
import { toLspRange } from '../utils/misc';
import { AncestryBuilder, getPathAt, isTaskParam } from '../utils/yaml';
import {
AncestryBuilder,
getOrigRange,
getPathAt,
isTaskParam,
parseAllDocuments,
} from '../utils/yaml';

export async function getDefinition(
document: TextDocument,
Expand All @@ -26,11 +31,12 @@ export async function getDefinition(
document.uri
);
if (module) {
const range = getOrigRange(node);
return [
{
targetUri: module.source,
originSelectionRange: node.range
? toLspRange(node.range, document)
originSelectionRange: range
? toLspRange(range, document)
: undefined,
targetRange: Range.create(
module.sourceLineRange[0],
Expand Down
13 changes: 7 additions & 6 deletions src/providers/hoverProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Hover, MarkupContent, MarkupKind } from 'vscode-languageserver';
import { Position, TextDocument } from 'vscode-languageserver-textdocument';
import { parseAllDocuments } from 'yaml';
import { Scalar, YAMLMap } from 'yaml/types';
import { DocsLibrary } from '../services/docsLibrary';
import {
Expand All @@ -19,11 +18,13 @@ import { toLspRange } from '../utils/misc';
import {
AncestryBuilder,
findProvidedModule,
getOrigRange,
getPathAt,
isBlockParam,
isPlayParam,
isRoleParam,
isTaskParam,
parseAllDocuments,
} from '../utils/yaml';

export async function doHover(
Expand Down Expand Up @@ -60,23 +61,22 @@ export async function doHover(
path,
document.uri
);
const range = getOrigRange(node);
if (module && module.documentation) {
return {
contents: formatModule(
module.documentation,
docsLibrary.getModuleRoute(hitFqcn || node.value)
),
range: node.range ? toLspRange(node.range, document) : undefined,
range: range ? toLspRange(range, document) : undefined,
};
} else if (hitFqcn) {
// check for tombstones
const route = docsLibrary.getModuleRoute(hitFqcn);
if (route) {
return {
contents: formatTombstone(route),
range: node.range
? toLspRange(node.range, document)
: undefined,
range: range ? toLspRange(range, document) : undefined,
};
}
}
Expand Down Expand Up @@ -136,9 +136,10 @@ function getKeywordHover(
}
: keywordDocumentation;
if (markupDoc) {
const range = getOrigRange(node);
return {
contents: markupDoc,
range: node.range ? toLspRange(node.range, document) : undefined,
range: range ? toLspRange(range, document) : undefined,
};
} else return null;
}
10 changes: 6 additions & 4 deletions src/providers/semanticTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
SemanticTokenTypes,
} from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { parseAllDocuments } from 'yaml';
import { Node, Pair, Scalar, YAMLMap, YAMLSeq } from 'yaml/types';
import { IModuleMetadata } from '../interfaces/module';
import { DocsLibrary } from '../services/docsLibrary';
Expand All @@ -17,10 +16,12 @@ import {
} from '../utils/ansible';
import {
findProvidedModule,
getOrigRange,
isBlockParam,
isPlayParam,
isRoleParam,
isTaskParam,
parseAllDocuments,
} from '../utils/yaml';

export const tokenTypes = [
Expand Down Expand Up @@ -233,9 +234,10 @@ function markNode(
builder: SemanticTokensBuilder,
document: TextDocument
) {
if (node.range) {
const startPosition = document.positionAt(node.range[0]);
const length = node.range[1] - node.range[0];
const range = getOrigRange(node);
if (range) {
const startPosition = document.positionAt(range[0]);
const length = range[1] - range[0];
builder.push(
startPosition.line,
startPosition.character,
Expand Down
12 changes: 9 additions & 3 deletions src/providers/validationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
Range,
} from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { parseAllDocuments } from 'yaml';
import { ValidationManager } from '../services/validationManager';
import { WorkspaceFolderContext } from '../services/workspaceManager';
import { parseAllDocuments } from '../utils/yaml';

/**
* Validates the given document.
Expand Down Expand Up @@ -63,8 +63,14 @@ function getYamlValidation(textDocument: TextDocument): Diagnostic[] {
const errorRange = error.range || error.source?.range;
let range;
if (errorRange) {
const start = textDocument.positionAt(errorRange.start);
const end = textDocument.positionAt(errorRange.end);
const start = textDocument.positionAt(
errorRange.origStart !== undefined
? errorRange.origStart
: errorRange.start
);
const end = textDocument.positionAt(
errorRange.origEnd !== undefined ? errorRange.origEnd : errorRange.end
);
range = Range.create(start, end);

let severity;
Expand Down
2 changes: 1 addition & 1 deletion src/services/ansibleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class AnsibleConfig {

const ansibleConfigResult = child_process.execSync(ansibleConfigCommand, {
encoding: 'utf-8',
cwd: new URL(this.context.workspaceFolder.uri).pathname,
cwd: decodeURI(new URL(this.context.workspaceFolder.uri).pathname),
env: ansibleConfigEnv,
});

Expand Down
Loading