跳到正文
cccskills
免费在 GitHub 公开中文介绍

poly-context

帮助在 IntelliJ 平台中实现 `PolyContext`,判断文件或代码位置适用的环境与功能。可处理缓存检测器、声明式规则和 `.ws-context` 用户覆盖设置。

原文查看原文说明

Implement PolyContext feature gating and environment detection.

JetBrains/intellij-community星标 20,684(GitHub 20,684 + cccskills 0)

实测评分 未实测

查看安装方法

适用场景

  • 判断代码位置适用的框架
  • 按环境切换功能注册
  • 按文件或依赖定义条件
  • 允许用户覆盖上下文值

中文介绍

功能

本技能帮助在 IntelliJ 平台上实现 PolyContext,判断某个文件或代码位置适用的开发环境或功能。它会说明如何在文件解析前进行判断,以及如何通过语法树判断,并介绍两种方式的适用情况。内容还包括使用缓存的检测提供器、通过 Web Types JSON 或代码定义声明式规则,以及用 .ws-context 文件允许用户覆盖上下文值。

适用场景

适合在插件中根据项目包含的框架或软件包启用相应功能。该方法也适用于网页开发以外的环境判断,例如检查 Ruby gem。当多个功能各自重复检查相同条件时,可以集中定义规则,减少重复判断。

使用示例

  • “说明如何判断此文件中启用的框架。”
  • “添加一条根据软件包是否存在来切换上下文的规则。”
  • “说明如何考虑 .ws-context 中的用户设置。”

注意事项

此技能以 IntelliJ 平台插件开发为前提。检测结果会被频繁查询,因此提供器需要使用缓存。VirtualFile 和 Project 方式无法访问语法树,解析后的结果可能与文件解析前不同。

本介绍由 AI(OpenAI Codex)根据公开的 SKILL.md 撰写。准确的规格请查看下方原文。

文档检查

AI 根据 SKILL.md 的内容检查了需要注意的操作和使用前提。评分只依据实际运行的结果(实测对比)。

安全性发送数据、删除文件、执行命令等是否有说明
A良好资料描述的是上下文检测和缓存,未提及敏感操作。
安装难易度所需工具和 API 密钥是否少且有说明
B部分不足需要进行 IntelliJ 平台插件开发,但未提供安装步骤。

不计入评分。 由 AI(OpenAI Codex)于 2026年10月28日 评估。仅供参考,安装前请同时查看原文。

如果您认为评估有误,请申请复核

请写明哪一项有误以及原因。无需登录。我们会对照 SKILL.md 重新评估,并在此处更新结果。

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

包含的文件(1)

  • SKILL.md8.2 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

<!-- Generated by community/.ai/render-guides.mjs; edit community/.agents/skills/poly-context/SKILL.md -->

PolyContext

PolyContext answers "what coding environment am I in right now." It is a standalone platform mechanism — poly-symbols is its biggest consumer (gating which PolySymbolQueryScopeContributor/QueryConfigurator registrations apply), but PolyContext itself has no dependency on PolySymbols or on web frameworks being involved at all. Its defining advantage is that it is heavily optimized for performance (cached, cheap, RequiresReadLock-only), which makes it the right tool for any "is feature X relevant here" check that would otherwise get reinvented per-feature as an ad-hoc cached provider — not just "which JS framework is active." Confirmed non-web examples already in this repo: PolyContext.PKG_MANAGER_RUBY_GEMS/ PKG_MANAGER_SYMFONY_BUNDLES constants live right next to PKG_MANAGER_NODE_PACKAGES in PolyContext.kt, and ruby/tests/.../gem/GemContextTest.kt queries a "timecop-context" kind (is the timecop Ruby gem present?) with no PolySymbols query executor or web framework in sight.

For any given context kind (e.g. "framework"), at most one name is active at a location, or null if none applies.

PolyContext.get("framework", psiElement) // "vue" | "angular" | "astro" | null

Two location overloads

PolyContext.get(kind: PolyContextKind, location: PsiElement): PolyContextName?
PolyContext.get(kind: PolyContextKind, location: VirtualFile, project: Project): PolyContextName?
LocationCostCapability
PsiElementnormalcan inspect the PSI tree (e.g. check imports) — use for most coding-assistance logic
VirtualFile + Projectcheap, indexing-safeno PSI access; results may legitimately differ from the PsiElement answer for the same file once parsed

The VirtualFile+Project overload is what makes PolyContext usable during language substitution — i.e. before any PSI exists for the file, when the platform is still deciding which Language to parse it as. Concrete chain in this repo: WebFrameworkHtmlLanguageSubstitutor.getLanguage(file: VirtualFile, project: Project) (a LanguageSubstitutor, plugins/JavaScriptLanguage/web-platform/.../WebFrameworkHtmlLanguageSubstitutor.kt:15) → WebFramework.forContext(file, project) → PolySymbolFramework.inLocation(file, project) (community/platform/polySymbols/shared/src/com/intellij/polySymbols/framework/PolySymbolFramework.kt:52-53) → PolyContext.get(KIND_FRAMEWORK, file, project). The result picks which HTML dialect (Vue/Angular/plain) the file's Language is substituted to, entirely before a PSI tree is built.

Canonical PolySymbols use case: web framework detection. Only one of Vue/Angular/React/Astro can be the active framework at a location, and each plugin's PolySymbolQueryScopeContributor/ QueryConfigurator gates its registrations with .inContext { it.framework == MyFramework.ID } (see poly-symbols/references/query-model.md) — but this is one consumer among many, not the reason PolyContext exists.

PolyContextProvider — direct/dynamic detection

Implement isEnabled() (or isForbidden()) and register at EP com.intellij.polySymbols.context. Results are called very often — cache them (CachedValuesManager).

PolyContext.get("stimulus-context", psiElement) == "true"

Blocking: isForbidden() with name "any" suppresses an entire context kind — e.g. a Python templating (Blade-style) blocker disables the whole framework kind so it never conflicts with Vue/Angular detection.

Real examples in this repo/docs: StimulusContextProvider (checks for a JS import, caches the result), PyTemplatesWebContextBlocker (forbids framework when Python templating is detected). For a full custom-detector example, see VueFileContextProvider (contrib/vuejs/vuejs-backend/src/org/jetbrains/vuejs/context/VueFileContextProvider.kt) — returns true unconditionally for .vue files, and for any HTML-compatible file whose <script src="..."> matches a known Vue CDN/filename pattern (a cached-value scan, not a per-call check). Angular's equivalent is AngularCliContextProvider (contrib/Angular/.../org/angular2/cli/), registered at polySymbols.context kind="framework" name="angular".

Context rules — the declarative, faster alternative

Many providers redundantly re-check the same thing (is package X a dependency?). Context rules are declarative and let the platform compute proximity once instead of running N ad-hoc providers.

Via Web Types

Under a top-level contexts-config property in a Web Types JSON file (see poly-symbols/references/web-types.md): kind → name → enable-when/disable-when. (Pre-2024.2 files use a deprecated layout where name is top-level and kind a sibling property — don't use that shape for new files.)

enable-when rule kinds: file-extensions, file-name-patterns, ide-libraries, project-tool-executables, node-packages, ruby-gems. disable-when is more limited: file-extensions, file-name-patterns only.

Proximity scoring (lower = closer/stronger match; the winning name for a kind is whichever rule has the lowest total):

  • file-extensions / file-name-patterns → 0.0 (perfect match)
  • ide-libraries / ruby-gems → Double.MAX_VALUE (project/module-level match)
  • node-packages → computed from package.json location + dependency type: same directory as base 0.0, +1.0 per parent directory walked up, plus importance modifiers — peerDependencies +0.1, bundledDependencies +0.2, dependencies +0.3, optionalDependencies +0.4, devDependencies +0.5, indirect (node_modules) +0.6.

Web Types shipping a context rule are registered at EP com.intellij.polySymbols.webTypes (currently Node Package Manager only, requires the JS plugin) with two naming strategies: name the file after the real npm package if the rule should trigger on that package's presence, or use an arbitrary name + enableByDefault="true" if it should always apply.

Via code — PolyContextRulesProvider

For rules that can't be expressed as static file-based data, implement a PolySymbolQueryConfigurator and override getContextRulesProviders(project, dir): List<PolyContextRulesProvider> at EP com.intellij.polySymbols.queryConfigurator. Rule output must be stable — a change triggers project rescanning and cache invalidation.

.ws-context — user override file

Since 2024.1.2, users can force context values with a .ws-context JSON file:

  • <context kind> → <context name> — direct top-level assignment (implicitly applies to /**).
  • <GLOB path> → nested context-details object — GLOB supports only * in the final segment; ** matches nested directories.

When multiple patterns match a file, resolution priority is: (1) most path segments excluding **, (2) prefer patterns with an actual file-name match (not ending in **//), (3) first-defined wins.

PolyContextSourceProximityProvider

For integrating a new package manager or a language with a global-library concept, register at EP com.intellij.polySymbols.contextSourceProximityProvider and implement calculateProximity(), computing proximity per sourceNames entry matching a sourceKind (e.g. a dependency type). Return a Result with modificationTrackers — keep the tracker count minimal since it feeds every PolyContext.get() call.

Related

  • poly-symbols — PolyContext's biggest consumer, not something it depends on.
  • poly-symbols/references/web-types.md — contexts-config file format.
  • Official docs: Poly Symbols Context (the only official doc page for this API — despite the URL/page name, treat it as documenting a general-purpose context mechanism, not something PolySymbols-exclusive).

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

actions

免费有简介

实现或修改 IntelliJ `AnAction`,并在 `plugin.xml` 中注册。将显示名称和说明写入消息资源文件,按需设置图标,并通过事件协程作用域启动挂起任务,避免同步处理阻塞 IDE。

  • 为 IDE 菜单添加操作
  • 修改操作注册或图标
  • 整理操作名称和说明
JetBrains/intellij-community2.1万2026年10月29日 更新

bazel-test-migration

免费有简介

将 IntelliJ JPS 测试模块迁移到 Bazel,并排查仅在 Bazel 中出现的失败。根据症状检查依赖、插件、测试数据和 runfiles,重新生成并验证目标。

  • 将 JPS 测试模块迁移到 Bazel
  • 排查仅在 Bazel 中失败的测试
  • 检查测试依赖和数据配置
JetBrains/intellij-community2.1万2026年10月29日 更新

code-style

免费有简介

按 IntelliJ 项目规范编写或审查 Kotlin 和 Java 代码,涵盖语言选择、格式、密封类型、线程注解及控制流异常处理。

  • 按 IntelliJ 规范编写 Kotlin
  • 审查 Java 代码
  • 检查异常处理和密封类型分支
JetBrains/intellij-community2.1万2026年10月29日 更新

commits

免费有简介

本指南帮助您按 IntelliJ 项目规则拆分更改并撰写提交信息:有工单时使用编号和变更说明,无工单时使用标签与子系统名,并在需要时说明原因和决定。

  • 按项目格式编写带工单号的提交信息
  • 整理测试或文档提交并选择合适标签
  • 按规则修改已有提交信息
JetBrains/intellij-community2.1万2026年10月29日 更新

使用多个 Python 类型检查器比较代码片段或文件的类型推断和报错判断,并汇总各工具的结论、耗时与诊断信息,可保存为 Markdown 报告。

  • 比较不同检查器的类型推断
  • 确认代码是否会被判为类型错误
  • 查看多个工具的诊断差异
JetBrains/intellij-community2.1万2026年10月29日 更新

整理 Compose Desktop 界面中的状态和副作用,让可组合函数负责绘制,由展示器或协调器处理应用决策。了解状态归属、Effect、可复用组件接口、延迟列表键、动画和性能,适用于单项界面修改。

  • 分离界面绘制与展示器决策
  • 整理界面状态的归属
  • 检查 Effect 键和协程用法
JetBrains/intellij-community2.1万2026年10月29日 更新

查看 JetBrains 的全部技能

报告此技能的问题