IT이야기

VS 코드 확장 - 전체 경로 가져오기

cyworld 2022. 3. 21. 09:01
반응형

VS 코드 확장 - 전체 경로 가져오기

VS Code용 플러그인을 작성 중인데, 확장명을 호출하는 파일이 편집자의 상황에 맞는 메뉴나 탐색기의 상황에 맞는 메뉴에서 호출되었거나 사용자가 확장명 명령만 입력한 경우 경로를 알아야 한다.

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

미리 고마워!

파일 사용이 필요한 경우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);
}

VS Code API를 통해 보다 자세한 정보 제공

vscode 창 속성을 호출하여 찾고 있는 항목에 따라 파일 경로나 이름을 검색할 수 있다.이렇게 하면 명령을 실행할 때 현재 탭에 열려 있는 파일의 이름이 표시된다.탐험가 컨텍스트에서 호출하면 어떻게 작동하는지 모르겠다.

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;

위의 코드는 현재 vscode에서 활성화된 파일의 경로를 찾는 데 사용된다.

vscode.window.activeTextEditor편집자의 참조를 가져오고document.uri.fsPath문자열 형식으로 해당 파일의 경로 반환

다음은 윈도우즈에서 vscode로 반환된 다양한 경로의 예:

확장 경로:

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

현재 폴더:

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

현재 편집기 파일:

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 

참고:path그리고fsPathfsPath는 OS에 적합한 형태로 경로를 제공한다.

참조URL: https://stackoverflow.com/questions/39569993/vs-code-extension-get-full-path

반응형