Enable auto-switching between yaml and ansible language
See original GitHub issueProblem
As one of the most common confusions with vscode extension is the fact that vscode often fails to decide which document language to use between generic yaml
and ansible
language. That is because at this moment only filename patterns are use to determine if a file uses one language or another. Once user manually change the language the IDE remembers the selection, but our goal is to avoid the need for this manual selection.
With @ganeshrn help, we identified several existing extensions that hook into document open event and decide to change the language of the document based on the file content, such the shebang line for example:
- https://marketplace.visualstudio.com/items?itemName=davidhewitt.shebang-language-associator (updated 2022)
- https://marketplace.visualstudio.com/items?itemName=ryzngard.vscode-header-source (updated 2019)
Solution
We need to implement the same hook for document open even and when the current document is using either yaml
or ansible
, use a detection function that is guessing if the file is a playbook or a taskfile.
We currently have the isPlaybook
function but we need a new one named detectDocumentLanguage
which receives the open document and returns the name of the vscode language that vscode should use for it.
The function should return null
if we were not able to make a decision. For example if current document is not YAML or the YAML loading fails, we are expected to return null, as we cannot make a decision.
Once the document is loaded, the logic should be:
- if current document language is not already
ansible
oryaml
, returnnull
. - if document is
null
, empty list, string or empty mapping, returnnull
(inconclusive) - if document is a non empty mapping, return
yaml
- if document is a non empty sequence, check if the first element is a dictionary having either one of the following keys:
name
,hosts
,import_playbook
, returnansible
. - return
null
as fallback, as we do not want to auto-switch document type if we are not confident enough.
Alternatives
No response
Additional context
Please look at current extensions that do this switching as all of them are open source and the implementation for document open is quite easy.
This feature does not require us to touch ansible-language-server project, as this hook is specific to vscode client.
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:12 (11 by maintainers)
Yeah, not required. But when someone shots himself in the foot, you cannot blame the weapon. In fact ansible can also load files with any extensions, yml or yaml not being required, but that does not mean we can support anything that happens to work.
Once we fix the auto-detection users will be VERY happy!
Would it be possible to apply the schemata for ansible content to y(a)ml files as a strong hint about their “language”? A file that doesn’t match them could still be an ansible file, but ones that match very likely are true positives.
So far it still seems like a file such as
roles/tasks/main.yml
will likely be miscategorized asyaml
as it doesn’t refer tohosts
orimport_playbook
.Would a patch to extend https://github.com/ansible/vscode-ansible/blob/038b856fe98cab77adc7f73ca728d93474c29cd4/package.json#L297-L301 be welcome that at least adds
"**/tasks/*.y(a)ml"
and"**/handlers/*.y(a)ml"
?