英文:
Using getWordUnderCursor to get words that contain apostrophes?
问题
我在一个 Electron 应用程序中使用 monaco-editor
,用户将输入诗歌。编辑器的语言设置为 plaintext
。
我遇到了一个问题,如果用户输入了一个带有 '(撇号)的单词,getWordAtPosition
不会返回完整的单词。根据光标的位置(我将其传递给 getWordAtPosition
),它将返回撇号之前的内容,或者撇号之后的内容。
例如,使用 |(竖线)表示光标位置,如果用户输入了:
couldn't|
getWordAtPosition
将返回 t
对于 |couldn't
,它将返回 couldn
。
我想要的是,无论光标位置如何,它都应该返回 couldn't
。
到目前为止,我尝试了在创建编辑器时在选项中提供 wordSeparators
,不包括撇号(但是其他方面保持默认设置),但这并没有帮助。
编辑器创建代码块:
editorInstance = monaco.editor.create(document.getElementById('editor'), {
language: 'plaintext',
wordSeparators: '`~!@#$%^&*()-=+[{]}\\|;:"<>,/?'
});
使用 getWordAtPosition
的代码块:
fromEventPattern((handler: NodeEventHandler) => editorInstance.onDidChangeCursorPosition(handler))
.pipe(
map(() => {
const cursorPosition: IPosition = editorInstance.getPosition();
return editorInstance.getModel()
.getWordAtPosition(cursorPosition)
?.word;
}),
filter((value: string) => !!value)
);
非常感谢您的帮助!提前感谢!
英文:
I'm using monaco-editor
in an Electron application where users will be typing poetry. The language set for the editor is plaintext
.
I'm running into an issue where if a user has typed a word with an ' (apostrophe) in it, getWordAtPosition
won't return the full word. Depending on the cursor's position (which is what I pass into getWordAtPosition
), it'll either return what's before the apostrophe, or what's after it.
For example, using a | (pipe) to represent the cursor's location, if the user has typed:
couldn't|
getWordAtPosition
will return t
For |couldn't
, it will return couldn
.
What I want is for, regardless of cursor position, it will return couldn't
.
What I've tried so far is supplying wordSeparators
in the options when creating the editor that doesn't include the apostrophe (but is otherwise the default), but that hasn't helped.
Editor creation block:
editorInstance = monaco.editor.create(document.getElementById('editor'), {
language: 'plaintext',
wordSeparators: '`~!@#$%^&*()-=+[{]}\\|;:",.<>/?'
});
Block using the getWordAtPosition
:
fromEventPattern((handler: NodeEventHandler) => editorInstance.onDidChangeCursorPosition(handler))
.pipe(
map(() => {
const cursorPosition: IPosition = editorInstance.getPosition();
return editorInstance.getModel()
.getWordAtPosition(cursorPosition)
?.word;
}),
filter((value: string) => !!value)
);
Any help would be greatly appreciated! Thanks in advance!
答案1
得分: 1
你可以设置语言配置(无论是为新语言还是现有语言),并提供一个`wordPattern`正则表达式,用于匹配单词。
monaco.languages.setLanguageConfiguration('plaintext', {
wordPattern: /'?\w[\w'-.][?!,;:""]/
});
monaco.editor.create(document.getElementById("container"), {
language: 'plaintext',
});
尽管`plaintext`已经存在作为一种语言,但这仍然有效。我不确定`setLanguageConfiguration`是否执行任何合并(我假设不会?),所以请牢记这一点。
我的代码片段中的正则表达式并不完美,但它可以匹配类似以下的单词:
* Couldn't
* 'cause
* you?
* Hello
如果您是未来搜索者,并且查看此答案,请记住这一点!
英文:
You can set a language configuration (either for a new language, or an existing one) and provide a wordPattern
regex that will be used to match against words.
monaco.languages.setLanguageConfiguration('plaintext', {
wordPattern: /'?\w[\w'-.]*[?!,;:"]*/
});
monaco.editor.create(document.getElementById("container"), {
language: 'plaintext',
});
Even though plaintext
already exists as a language, this still works. I'm not sure if setLanguageConfiguration
does any merging (I'd assume not?) so keep that in mind.
The regex in my code snippet isn't perfect, but it matches words like:
- Couldn't
- 'cause
- you?
- Hello
which was important for my use-case. Keep that in mind if you're a future searcher looking at this answer!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论