如何让 Sublime Text 插件覆盖用户的默认 "auto_complete_selector" 设置?
How to have a Sublime Text plugin override the user's default "auto_complete_selector" setting?
这样做的基本原理是构建一个 ESLint 规则自动完成插件,但当然,我不希望在我的评论之外建议 ESLint 规则自动完成,我也不希望必须启用所有建议在评论中。
我正在尝试复制行为 like in this VSCode plugin for the same purpose.
我也不完全确定此行为是否需要自动完成、片段或两者都需要?
对于那些不了解 ESLint 注释语法的人,它是这样的:
var a = 1; // eslint-disable-line RULE_NAME
鉴于您设置的条件,即
- 完成的内容应该只出现在评论中。
- 补全应仅出现在某些单词之后(在本例中为
eslint-disable
和 family)。
您需要编写一个自定义插件来提供补全并跟踪补全的显示位置。
以下是完成此操作的插件(它模仿 VS Code 的对应部分)
import sublime
import sublime_plugin
class ESLintListener(sublime_plugin.EventListener):
eslint_completions = ["camelcase","default-case","func-names","global-require","import/prefer-default-export","indent","max-len","new-cap","no-alert","no-cond-assign","no-confusing","no-console","no-extend-native","no-mixed-operators","no-new","no-param-reassign","no-shadow","no-undef","no-unused-vars","prefer-arrow-callback","prefer-rest-params","react/prop-types","wrap-iife"]
def on_query_completions(self, view, prefix, locations):
if not view.match_selector(locations[0] - 1, "source.js comment"):
return
if (
view.substr(sublime.Region(locations[0] - 16, locations[0] - 2)) == "eslint-disable" or
view.substr(sublime.Region(locations[0] - 21, locations[0] - 2)) == "eslint-disable-line" or
view.substr(sublime.Region(locations[0] - 26, locations[0] - 2)) == "eslint-disable-next-line"
):
return [
sublime.CompletionItem(
trigger=item,
annotation=item,
details="Eslint rule",
kind=(sublime.KIND_ID_FUNCTION, "E", "Eslint")
)
for item in self.eslint_completions
]
return None
第一个 if
条件检查条件 (1),第二个条件检查条件 (2),如果匹配则 returns 所需的完成。
请注意,这不会自动完成 eslint
相关词,而只会自动完成之后的规则名称。
我使用了一些新的 ST4 功能只是为了一点点趣味(同时也提供了更多关于补全的细节)。
为了使其正常工作,您需要在 auto_complete_selector
中包含 source.js comment
,因为默认情况下,评论中禁用完成。
编辑(基于评论):
为了避免单词和显式完成,我们使用如下所示的两个标志。这应该会在某种程度上减轻 AC 中显示的其他完成情况。
import sublime
import sublime_plugin
class ESLintListener(sublime_plugin.EventListener):
eslint_completions = ["camelcase","default-case","func-names","global-require","import/prefer-default-export","indent","max-len","new-cap","no-alert","no-cond-assign","no-confusing","no-console","no-extend-native","no-mixed-operators","no-new","no-param-reassign","no-shadow","no-undef","no-unused-vars","prefer-arrow-callback","prefer-rest-params","react/prop-types","wrap-iife"]
def on_query_completions(self, view, prefix, locations):
if not view.match_selector(locations[0] - 1, "source.js comment"):
return
if (
view.substr(sublime.Region(locations[0] - 16, locations[0] - 2)) == "eslint-disable" or
view.substr(sublime.Region(locations[0] - 21, locations[0] - 2)) == "eslint-disable-line" or
view.substr(sublime.Region(locations[0] - 26, locations[0] - 2)) == "eslint-disable-next-line"
):
return (
[
sublime.CompletionItem(
trigger=item,
annotation=item,
details="Eslint rule",
kind=(sublime.KIND_ID_FUNCTION, "E", "Eslint")
)
for item in self.eslint_completions
],
sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS
)
return None
这样做的基本原理是构建一个 ESLint 规则自动完成插件,但当然,我不希望在我的评论之外建议 ESLint 规则自动完成,我也不希望必须启用所有建议在评论中。
我正在尝试复制行为 like in this VSCode plugin for the same purpose.
我也不完全确定此行为是否需要自动完成、片段或两者都需要?
对于那些不了解 ESLint 注释语法的人,它是这样的:
var a = 1; // eslint-disable-line RULE_NAME
鉴于您设置的条件,即
- 完成的内容应该只出现在评论中。
- 补全应仅出现在某些单词之后(在本例中为
eslint-disable
和 family)。
您需要编写一个自定义插件来提供补全并跟踪补全的显示位置。
以下是完成此操作的插件(它模仿 VS Code 的对应部分)
import sublime
import sublime_plugin
class ESLintListener(sublime_plugin.EventListener):
eslint_completions = ["camelcase","default-case","func-names","global-require","import/prefer-default-export","indent","max-len","new-cap","no-alert","no-cond-assign","no-confusing","no-console","no-extend-native","no-mixed-operators","no-new","no-param-reassign","no-shadow","no-undef","no-unused-vars","prefer-arrow-callback","prefer-rest-params","react/prop-types","wrap-iife"]
def on_query_completions(self, view, prefix, locations):
if not view.match_selector(locations[0] - 1, "source.js comment"):
return
if (
view.substr(sublime.Region(locations[0] - 16, locations[0] - 2)) == "eslint-disable" or
view.substr(sublime.Region(locations[0] - 21, locations[0] - 2)) == "eslint-disable-line" or
view.substr(sublime.Region(locations[0] - 26, locations[0] - 2)) == "eslint-disable-next-line"
):
return [
sublime.CompletionItem(
trigger=item,
annotation=item,
details="Eslint rule",
kind=(sublime.KIND_ID_FUNCTION, "E", "Eslint")
)
for item in self.eslint_completions
]
return None
第一个 if
条件检查条件 (1),第二个条件检查条件 (2),如果匹配则 returns 所需的完成。
请注意,这不会自动完成 eslint
相关词,而只会自动完成之后的规则名称。
我使用了一些新的 ST4 功能只是为了一点点趣味(同时也提供了更多关于补全的细节)。
为了使其正常工作,您需要在 auto_complete_selector
中包含 source.js comment
,因为默认情况下,评论中禁用完成。
编辑(基于评论):
为了避免单词和显式完成,我们使用如下所示的两个标志。这应该会在某种程度上减轻 AC 中显示的其他完成情况。
import sublime
import sublime_plugin
class ESLintListener(sublime_plugin.EventListener):
eslint_completions = ["camelcase","default-case","func-names","global-require","import/prefer-default-export","indent","max-len","new-cap","no-alert","no-cond-assign","no-confusing","no-console","no-extend-native","no-mixed-operators","no-new","no-param-reassign","no-shadow","no-undef","no-unused-vars","prefer-arrow-callback","prefer-rest-params","react/prop-types","wrap-iife"]
def on_query_completions(self, view, prefix, locations):
if not view.match_selector(locations[0] - 1, "source.js comment"):
return
if (
view.substr(sublime.Region(locations[0] - 16, locations[0] - 2)) == "eslint-disable" or
view.substr(sublime.Region(locations[0] - 21, locations[0] - 2)) == "eslint-disable-line" or
view.substr(sublime.Region(locations[0] - 26, locations[0] - 2)) == "eslint-disable-next-line"
):
return (
[
sublime.CompletionItem(
trigger=item,
annotation=item,
details="Eslint rule",
kind=(sublime.KIND_ID_FUNCTION, "E", "Eslint")
)
for item in self.eslint_completions
],
sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS
)
return None