简体   繁体   English

VS 代码扩展 - 获取完整路径

[英]VS Code extension - get full path

I am writing a plugin for VS Code and I need to know the path of the file which is calling the extension, either if it was called from the editor context menu or explorer context menu or the user simply typed the extension command.我正在为 VS Code 编写一个插件,我需要知道调用扩展的文件的路径,无论它是从编辑器上下文菜单或资源管理器上下文菜单中调用的,还是用户只是键入扩展命令。

function activate(context){
    // get full path of the file somehow
}

Thanks in advance!提前致谢!

If you need the File use uri.fsPath If you need the Workspace Folder use uri.path如果您需要文件,请使用uri.fsPath如果您需要工作区文件夹,请使用uri.path

if(vscode.workspace.workspaceFolders !== undefined) {
    let wf = vscode.workspace.workspaceFolders[0].uri.path ;
    let f = vscode.workspace.workspaceFolders[0].uri.fsPath ; 

    message = `YOUR-EXTENSION: folder: ${wf} - ${f}` ;

    vscode.window.showInformationMessage(message);
} 
else {
    message = "YOUR-EXTENSION: Working folder not found, open a folder an try again" ;

    vscode.window.showErrorMessage(message);
}

More detail can get from VS Code API更多细节可以从VS Code API 获得

You can invoke the vscode window property to retrieve the file path or name depending on what you are looking for.您可以调用 vscode 窗口属性来检索文件路径或名称,具体取决于您要查找的内容。 This will give you the name of the file open in the current Tab when you execute the command.当您执行命令时,这将为您提供在当前选项卡中打开的文件的名称。 I don't know how it works if called from the explorer context.如果从资源管理器上下文调用,我不知道它是如何工作的。

var vscode = require("vscode");
var path = require("path");
function activate(context) {
   var currentlyOpenTabfilePath = vscode.window.activeTextEditor.document.fileName;
   var currentlyOpenTabfileName = path.basename(currentlyOpenTabfilePath);
   //...
}
import * as vscode from "vscode";
import * as fs from "fs";   

var currentlyOpenTabfilePath = vscode.window.activeTextEditor?.document.uri.fsPath;

The above code is used to find the the path of file that is currently activated on vscode.以上代码用于查找当前在vscode上激活的文件路径。

vscode.window.activeTextEditor gets editor's reference and document.uri.fsPath returns the path to that file in string format vscode.window.activeTextEditor获取编辑器的引用, document.uri.fsPath以字符串格式返回该document.uri.fsPath的路径

Here are examples of various paths returned by vscode in windows:以下是 windows 中 vscode 返回的各种路径的示例:

Extension path:扩展路径:

vscode.extensions.getExtension('extension.id').extensionUri.path
> /c:/Users/name/GitHub/extensionFolder 
vscode.extensions.getExtension('extension.id').extensionUri.fsPath
> c:\Users\name\GitHub\extensionFolder

Current folder:当前文件夹:

vscode.workspace.workspaceFolders[0].uri.path
> /c:/Users/name/Documents/Notes 
vscode.workspace.workspaceFolders[0].uri.fsPath
> c:\Users\name\Documents\Notes 

Current editor file:当前编辑器文件:

vscode.window.activeTextEditor.document.uri.path
> /c:/Users/name/Documents/Notes/temp.md 
vscode.window.activeTextEditor.document.uri.fsPath
> c:\Users\name\Documents\Notes\temp.md 

Note that path and fsPath refer to the same folder.请注意, pathfsPath指的是同一个文件夹。 fsPath provides the path in the form appropriate for the os. fsPath 以适合操作系统的形式提供路径。

If you need the path to the current extension, use context object. This will work both in debug mode and production mode:如果您需要当前扩展的路径,请使用上下文 object。这在调试模式和生产模式下都有效:

export async function activate(context: ExtensionContext) {
  console.log(context.extension);
  console.log(context.extensionPath);
  console.log(context.extensionUri);
}

If you are going to call another extension:如果您要呼叫另一个分机:

extensions.getExtension('extensionId');

In debug mode, this returns undefined for current extension since it is not installed.在调试模式下,这将返回未定义的当前扩展,因为它没有安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM