rukaelin
rukaelin

Reputation: 268

determining the language of a file from its filepath

In my VSCode extension I have a string filePath and need to know its associated language.

As the user can change the language associations in the configuration (files.associations) only checking for the known extensions does not work.

Is there a functionality in the VSCode API to do that? Or do I need to extract the information from the configuration using vscode.workspace.getConfiguration("files").get("associations")?

Upvotes: 0

Views: 103

Answers (1)

Matt Bierner
Matt Bierner

Reputation: 65223

Try using workspace.openTextDocument and document.languageId:

import { workspace } from 'vscode';

workspace.openTextDocument(pathToMyFile).then(doc => {
    console.log(doc.languageId)
})

This only opens the document from the disk, it will not show it in the editor.

Upvotes: 1

Related Questions