[英]Reading another file in the same directory as the current file (VS Code)
I am creating a language server and currently have a list of static/preloaded completion items. 我正在创建语言服务器,当前有一个静态/预加载的完成项目列表。 This language server will activate on any
.script
file. 该语言服务器将在任何
.script
文件上激活。 In the same directory there will exist a .map
file (arbitrary extension, I am not referring to JS source map files). 在同一目录中,将存在一个
.map
文件(任意扩展名,我不是指JS源映射文件)。 The map file is just a plain text document that contains a list of classnames and names (for each 'entity'). 映射文件只是一个纯文本文档,其中包含类名和名称(每个“实体”)的列表。 A typical entry might look like this:
一个典型的条目可能如下所示:
// entity 237
{
"classname" "func_static"
"name" "func_static_237"
"origin" "200 179.5 -181"
"rotation" "0 -1 0 1 0 0 0 0 1"
}
I'm trying to add each name/classname pair to the completion list (name being the completion item label, and the classname being the detail). 我试图将每个名称/类名对添加到完成列表(名称是完成项标签,而类名是详细信息)。 Here is what I have done so far:
到目前为止,这是我所做的:
As there are unfortunately single cases of 'classname' on their own, I opted to use a regex that matches across both lines. 不幸的是,由于单独存在“类名”的单个情况,因此我选择使用跨两行都匹配的正则表达式。 Then I used two capture groups (one for name and one for classname) to push both into an array.
然后,我使用了两个捕获组(一个用于名称,一个用于类名称)将它们都推入数组。 I then iterate across each pair in the array and add it to the completion list.
然后,我遍历数组中的每一对,并将其添加到完成列表中。
The idea is that I am preloading variables from a map file into the corresponding script file (they'll both have the same filename and be in the same directory). 我的想法是,我正在将变量从映射文件预加载到相应的脚本文件中(它们都具有相同的文件名,并且位于同一目录中)。
The code looks like this: 代码如下:
'use strict';
import * as path from 'path';
import * as vscode from 'vscode';
import * as fs from 'fs';
export function activate(context: vscode.ExtensionContext) {
...
// each pair will go into this
var objArray = new Array;
function readTextFile() {
// load the map file
var text = fs.readFileSync(context.asAbsolutePath(path.join('donkey.map')));
// match classname and name
var regex = /\"classname\"\s\"(\w+)\"(?:\r\n|[\r\n])\"name\"\s\"(\w+)\"/g
getMatches(text.toString(), regex);
}
function getMatches(string: string, regex: RegExp) {
var match;
while (match = regex.exec(string)){
// capture group 1 = classname
// capture group 2 = name
// pushing name first because it's more important
objArray.push([match[2], match[1]]);
}
}
var loop1 = function (i: number) {
// create a new completion item
vscode.languages.registerCompletionItemProvider('quadscript', {
provideCompletionItems: function () {
var compItem = new vscode.CompletionItem(objArray[i][0], vscode.CompletionItemKind.Variable);
compItem.insertText = ("$" + objArray[i][0]);
compItem.detail = (objArray[i][1]);
return [
compItem
];
}
});
}
for (var i = 0; i < objArray.length; i++) {
loop1(i);
}
readTextFile();
}
When I debug the extension in the Extension Development Host, the debug console shows this: 在扩展开发主机中调试扩展时,调试控制台将显示以下内容:
C:\\Program Files\\Microsoft VS Code\\Code.exe --debugBrkPluginHost=37535 --debugId=cfbf2c6d-c313-46fa-8c44-4e64f44eb97f --extensionDevelopmentPath=c:\\Users\\thefyrewire\\Documents\\VSCode\\quadscript
C:\\ Program Files \\ Microsoft VS Code \\ Code.exe --debugBrkPluginHost = 37535 --debugId = cfbf2c6d-c313-46fa-8c44-4e64f44eb97f --extensionDevelopmentPath = c:\\ Users \\ fyrewire \\ Documents \\ VSCode \\ quadscript
And then when I type func they show up as expected showing they were sucessfully read from the file (which exists at c:\\Users\\thefyrewire\\Documents\\VSCode\\quadscript\\donkey.map
, corresponding to the extensionDevelopmentPath as seen in the debug console). 然后,当我键入func时,它们将按预期显示,表明它们已成功从文件中读取(该文件位于
c:\\Users\\thefyrewire\\Documents\\VSCode\\quadscript\\donkey.map
,与调试控制台中所示的extensionDevelopmentPath对应) )。
I next went to c:\\Users\\thefyrewire\\Documents\\test
and moved the donkey.script
and donkey.map
files here, then packaged and installed the extension. 接下来,我去
c:\\Users\\thefyrewire\\Documents\\test
并将donkey.script
和donkey.map
文件移到此处,然后打包并安装了扩展名。 When I went into the script file and brought up the completion list, the func_static items were not there. 当我进入脚本文件并显示完成列表时,func_static项不存在。 I think this is because it is trying to read from where the extension is installed in
.vscode\\extensions\\<extension>\\donkey.map
which now doesn't exist as I moved the file. 我认为这是因为它正在尝试从
.vscode\\extensions\\<extension>\\donkey.map
安装扩展名的位置读取,该位置在我移动文件时现在不存在。
So I tried to change the path from where I was reading the map file and used an absolute path as a test. 因此,我尝试更改读取地图文件的路径,并使用绝对路径作为测试。
var text = fs.readFileSync("file:///C:/Users/thefyrewire/Documents/test/donkey.map");
When debugging I get returned this: 调试时,我得到以下信息:
ENOENT: no such file or directory, open 'C:\\Program Files\\Microsoft VS Code\\file:\\C:\\Users\\thefyrewire\\Documents\\test\\donkey.map'.
ENOENT:没有此类文件或目录,请打开“ C:\\ Program Files \\ Microsoft VS Code \\ file:\\ C:\\ Users \\ thefyrewire \\ Documents \\ test \\ donkey.map”。 Close Warn
关闭警告
Using a relative path like so: 使用类似的相对路径:
var text = fs.readFileSync("./donkey.map");
Returns: 返回值:
ENOENT: no such file or directory, open 'C:\\Program Files\\Microsoft VS Code\\donkey.map'.
ENOENT:没有此类文件或目录,请打开“ C:\\ Program Files \\ Microsoft VS Code \\ donkey.map”。 Close Warn
关闭警告
I tried multiple other fs methods and it appears to only start reading from C:\\Program Files\\Microsoft VS Code
. 我尝试了多种其他fs方法,但它似乎仅从
C:\\Program Files\\Microsoft VS Code
开始读取。
So finally my question: what do I need to do in order to allow my extension to dynamically read from the same directory as whatever script file is loaded? 所以,最后是我的问题:为了使扩展名能够与加载了任何脚本文件的目录相同地动态读取,我该怎么办? Any advice would be greatly appreciated, thank you!
任何建议将不胜感激,谢谢!
Edit: I realise this is unclear. 编辑:我知道这还不清楚。 By 'script file' I don't mean a JS script file, it's again part of the custom language (hence the language server).
“脚本文件”并不是指JS脚本文件,它还是自定义语言的一部分(因此是语言服务器)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.