Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
{
"id": "emmylua.luarocks",
"name": "LuaRocks",
"when": "resourceExtname == .lua",
"when": "resourceExtname == .lua || resourceExtname == .rockspec",
"icon": "$(package)"
}
]
Expand Down Expand Up @@ -379,15 +379,15 @@
"commandPalette": [
{
"command": "emmylua.luarocks.installPackage",
"when": "resourceExtname == .lua"
"when": "resourceExtname == .lua || resourceExtname == .rockspec"
},
{
"command": "emmylua.luarocks.searchPackages",
"when": "resourceExtname == .lua"
"when": "resourceExtname == .lua || resourceExtname == .rockspec"
},
{
"command": "emmylua.luarocks.checkInstallation",
"when": "resourceExtname == .lua"
"when": "resourceExtname == .lua || resourceExtname == .rockspec"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion src/annotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode';
import { AnnotatorType } from './lspExtension';
import { LanguageClient } from 'vscode-languageclient/node';
import * as notifications from "./lspExtension";
import { get } from './configRenames';
import { get } from './configManager';

// 装饰器类型映射接口
interface DecorationMap {
Expand Down
39 changes: 21 additions & 18 deletions src/configRenames.ts → src/configManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as vscode from 'vscode';

/**
* Configuration key rename mappings
* Maps new keys to old keys for backward compatibility
* 配置项重命名映射, 新键名 -> 旧键名.
*
* 用于将旧配置键名映射到新配置键名, 以确保向后兼容性
*/
const CONFIG_RENAMES: ReadonlyMap<string, string> = new Map([
['emmylua.ls.executablePath', 'emmylua.misc.executablePath'],
Expand All @@ -11,18 +12,19 @@ const CONFIG_RENAMES: ReadonlyMap<string, string> = new Map([
]);

/**
* Track which deprecated configs have been warned about
* 已警告的废弃配置项
*/
const warnedDeprecations = new Set<string>();

/**
* Get configuration value with support for renamed keys
* Automatically falls back to old configuration keys for backward compatibility
* 获取配置值, 且支持重命名的键名.
*
* 会自动回退至旧配置键名, 确保向后兼容性.
*
* @param config - Workspace configuration
* @param key - Configuration key to retrieve
* @param defaultValue - Optional default value if config doesn't exist
* @returns Configuration value or default
* @param config - 配置
* @param key - 必须传入最新的配置键名以正确获取配置值
* @param defaultValue - 可选的默认值
* @returns 配置值或默认值
*/
export function get<T>(
config: vscode.WorkspaceConfiguration,
Expand All @@ -31,11 +33,11 @@ export function get<T>(
): T | undefined {
const oldKey = CONFIG_RENAMES.get(key);

// Check if old config exists and has a non-null value
// 如果旧键存在且有值, 那么我们需要使用旧键的值并警告用户该配置项已废弃
if (oldKey && config.has(oldKey)) {
const oldValue = config.get<T>(oldKey);
if (oldValue !== undefined && oldValue !== null) {
// Warn about deprecated config (only once per session)
// 如果用户没有警告过该配置项, 那么我们需要警告用户
if (!warnedDeprecations.has(oldKey)) {
warnedDeprecations.add(oldKey);
showDeprecationWarning(oldKey, key);
Expand All @@ -44,12 +46,12 @@ export function get<T>(
}
}

// Get from new config key
// 如果旧键不存在, 那么我们直接使用新键的值
return config.get<T>(key, defaultValue as T);
}

/**
* Show a deprecation warning for old configuration keys
* 显示配置项已废弃的警告
*/
function showDeprecationWarning(oldKey: string, newKey: string): void {
const message = `Configuration "${oldKey}" is deprecated. Please use "${newKey}" instead.`;
Expand All @@ -66,7 +68,7 @@ function showDeprecationWarning(oldKey: string, newKey: string): void {
}

/**
* Get configuration with proper typing and defaults
* 配置管理器
*/
export class ConfigurationManager {
private readonly config: vscode.WorkspaceConfiguration;
Expand All @@ -76,24 +78,25 @@ export class ConfigurationManager {
}

/**
* Get a configuration value with type safety
* 获取配置项
*/
get<T>(section: string, defaultValue?: T): T | undefined {
return get<T>(this.config, `${section}`, defaultValue) || get<T>(this.config, `emmylua.${section}`, defaultValue);
// `this.config`此时的值是所有`emmylua`配置项的集合(不包含`emmylua`前缀)
return get<T>(this.config, `${section}`, defaultValue);
}

/**
* Get language server executable path
*/
getExecutablePath(): string | undefined {
return this.get<string>('misc.executablePath');
return this.get<string>('ls.executablePath');
}

/**
* Get language server global config path
*/
getGlobalConfigPath(): string | undefined {
return this.get<string>('misc.globalConfigPath');
return this.get<string>('ls.globalConfigPath');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { LuaLanguageConfiguration } from './languageConfiguration';
import { EmmyContext } from './emmyContext';
import { IServerLocation, IServerPosition } from './lspExtension';
import { onDidChangeConfiguration } from './annotator';
import { ConfigurationManager } from './configRenames';
import { ConfigurationManager } from './configManager';
import * as Annotator from './annotator';
import { EmmyrcSchemaContentProvider } from './emmyrcSchemaContentProvider';
import { SyntaxTreeManager, setClientGetter } from './syntaxTreeProvider';
Expand Down
49 changes: 36 additions & 13 deletions src/languageConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LanguageConfiguration, IndentAction, IndentationRule, OnEnterRule } from 'vscode';
import { ConfigurationManager } from './configRenames';
import { ConfigurationManager } from './configManager';

/**
* Lua language configuration for VS Code
Expand All @@ -14,7 +14,7 @@ export class LuaLanguageConfiguration implements LanguageConfiguration {
// Configure annotation completion rules based on user settings
const configManager = new ConfigurationManager();
const completeAnnotation = configManager.isCompleteAnnotationEnabled();

this.onEnterRules = this.buildOnEnterRules(completeAnnotation);
this.indentationRules = this.buildIndentationRules();
this.wordPattern = this.buildWordPattern();
Expand All @@ -29,7 +29,7 @@ export class LuaLanguageConfiguration implements LanguageConfiguration {
{
beforeText: /^\s*function\s+\w*\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: {
action: {
indentAction: IndentAction.IndentOutdent,
appendText: '\t'
}
Expand All @@ -38,7 +38,7 @@ export class LuaLanguageConfiguration implements LanguageConfiguration {
{
beforeText: /^\s*local\s+\w+\s*=\s*function\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: {
action: {
indentAction: IndentAction.IndentOutdent,
appendText: '\t'
}
Expand All @@ -47,7 +47,7 @@ export class LuaLanguageConfiguration implements LanguageConfiguration {
{
beforeText: /^\s*.*\s*=\s*function\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: {
action: {
indentAction: IndentAction.IndentOutdent,
appendText: '\t'
}
Expand All @@ -56,34 +56,57 @@ export class LuaLanguageConfiguration implements LanguageConfiguration {
{
beforeText: /^\s*local\s+function\s+\w*\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: {
action: {
indentAction: IndentAction.IndentOutdent,
appendText: '\t'
}
}
];

// Add annotation completion rules if enabled
if (enableAnnotationCompletion) {
const annotationRules: OnEnterRule[] = [
// Continue annotation with space (---)
// 当前行以`--- `开头时, 自动补全`--- `
{
beforeText: /^---\s+/,
action: {
action: {
indentAction: IndentAction.None,
appendText: '--- '
}
},
// Continue annotation without space (---)
// 当前行以`---`开头时且后面没有任何内容时, 自动补全`---`
{
beforeText: /^---$/,
action: {
action: {
indentAction: IndentAction.None,
appendText: '---'
}
}
},
// 当前行以`-- `开头时, 自动补全`-- `
{
beforeText: /^--\s+/,
action: {
indentAction: IndentAction.None,
appendText: '-- '
}
},
// 当前行以一些注解标识符开头时, 自动补全`---@`
{
beforeText: /^---@(class|field|param|generic|overload)\b.*/,
action: {
indentAction: IndentAction.None,
appendText: '---@'
}
},
// 对`---@alias`多行格式的处理, 我们认为以`---|`开头的行都是`---@alias`的续行
{
beforeText: /^---\|.*/,
action: {
indentAction: IndentAction.None,
appendText: '---| '
}
},
];

return [...annotationRules, ...baseRules];
}

Expand Down
Loading