问题描述
我正在查看 Roslyn 的 CompletionService
和 ShouldTriggerCompletion
定义为
I'm looking at Roslyn's CompletionService
, and ShouldTriggerCompletion
is defined as
public virtual bool ShouldTriggerCompletion(
SourceText text,
int caretPosition,
CompletionTrigger trigger,
ImmutableHashSet<string> roles = null,
OptionSet options = null
) { … }
其中 CompletionTrigger
是单个 char 字符
的包装器.
where CompletionTrigger
is a wrapper for a single char Character
.
这似乎意味着我应该在键入时在每个字符上调用 ShouldTriggerCompletion
— 但这意味着我需要在每个字符上更新 SourceText
,这会分配一个 SourceText
code>TextChange,一个新的 SourceText
和其他可能的东西,取决于它的内部结构.
This seems to imply I should call ShouldTriggerCompletion
on each char when typing — however that would mean I need to update SourceText
on each char, which allocates an array of TextChange
, a new SourceText
and potentially other things depending on its internal structure.
我是否正确理解此 API?打字时使用它的最有效方法是什么?
Do I understand this API correctly? What is the most efficient way to use it when typing?
澄清一下,我知道我可以再猜测它,并且只将它称为 .
.但我的目标是按预期使用 API 的方式使用该 API,除已提供的优化外不进行任何优化.
To clarify, I know I can second-guess it and only call it for let's say .
. But my goal is to use the API the way it is intended to be used, without any optimizations other than those already provided.
推荐答案
是的,在 Visual Studio 中每次击键时,我们都会创建一个新的 SourceText 和一个新的文档/项目/解决方案快照.我们必须这样做,因为所有功能都取决于现有的 Document 实例.
Yes, on each keystroke in Visual Studio we create a new SourceText and a new Document/Project/Solution snapshot. We have to do it since all features are depending on that Document instance existing.
对我们来说,这样的操作很便宜.Visual Studio 中的文本编辑器组件已经为每次编辑创建了一个廉价的 ITextSnapshot
,它通过非常奇特的数据结构(想想字符串片段的二叉树)来实现,因此它尽可能便宜.当我们在编辑器中为文件创建 SourceTexts 时,我们改为创建我们的 自己的 SourceText 派生类型,我们只是将数据请求转发到编辑器 ITextSnapshot
API.SourceText 实际上拥有 ITextSnapshot
成员的一个子集并非巧合,因为我们正是为这种模式设计的!
For us, such an operation is cheap. The text editor component in Visual Studio already creates a cheap ITextSnapshot
for each edit, which it does via very fancy data structures (think binary trees of pieces of strings) so this is as cheap as it can be. When we create SourceTexts for files in the editor, we instead create our own derived type of SourceText and we just forward the requests for data to the editor ITextSnapshot
API. It's not a coincidence that SourceText has effectively a subset of ITextSnapshot
's members, because we were designing for precisely this pattern!
这篇关于键入时使用 Roslyn 的 CompletionSevice 的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!