简体   繁体   English

Visual Studio代码扩展:如何在拖动鼠标时停止触发onDidChangeTextEditorSelection事件?

[英]Visual Studio Code Extensions : How to stop firing onDidChangeTextEditorSelection events while dragging mouse?

I'm currently working on a line parsing extension that performs tasks based on the currently selected line and reparses when the user changes their selection. 我目前正在开发一种行解析扩展程序,该扩展程序根据当前选定的行执行任务,并在用户更改其选择时重新解析。

It doesn't make sense to update when the user is selecting more than one line, so everything fires inside a block that looks like: 当用户选择多个行时,进行更新是没有意义的,因此所有内容都会在类似于以下内容的块中触发:

if(editor.selection.isSingleLine){
    // Line Handling Here
}

However, what happens when the user is dragging the mouse to select the text that they want is that the event fires periodically since technically as the user drags over text, they are "selecting" slices of what they intend on selecting on the way there, some of which only consist of the line they started to drag on. 但是,当用户拖动鼠标以选择所需的文本时,会发生该事件定期触发,因为从技术上讲,当用户将鼠标悬停在文本上方时,他们正在“选择”他们打算在此处选择的内容的一部分,其中一些仅包含他们开始拖延的线。

It also parses twice for doubleclick, but that's probably fine because the selected word is on the same line anyways. 它也会为doubleclick解析两次,但这可能很好,因为无论如何所选单词都在同一行上。

For keyboard and commands, it behaves as intended, but I want it to only fire on a single click (re-positioning the cursor) or once the user is done dragging to select. 对于键盘和命令,它的行为符合预期,但我希望仅在单击(重新定位光标)时或在用户拖动完成后才触发它。

It may be a non-issue since the last selection will executed last and I could hack something together to use that, but that seems inelegant and may cause extremely poor performance since the line checking would be executed repeatedly. 由于最后一次选择将在最后执行,所以这可能不是问题,我可能会一起破解一些东西以使用它,但这似乎不太优雅,并且由于行检查会重复执行,可能会导致性能极差。

EDIT: I actually want to do something else if it's a multiline selection, so I do want to be able to get the final selection as well. 编辑:如果是多行选择,我实际上想做其他事情,所以我也希望能够获得最终选择。

The event is global and shared between all extensions. 该事件是全局的,并且在所有扩展之间共享。 For that reason a single extension cannot configure it. 因此,单个扩展无法配置它。 To handle the 'event avalanche' debouncing should be used. 为了处理“事件雪崩”,应使用反跳。 Instead of doing the work inside the event handler, schedule it to be done soon and cancel a potentially scheduled operation. 与其在事件处理程序内部进行工作,不如将其安排为尽快完成并取消可能的计划操作。 Like so 像这样

let handle;
function doWorkNow() {
  // parse what is selected
}
onDidChangeTextEditorSelection(() => {
  clearTimeout(handle);
  handle = setTimeout(() => doWorkNow(), 10);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM