【问题标题】:How to get the current token of java editor in a plugin如何在插件中获取 java 编辑器的当前令牌
【发布时间】:2015-11-03 01:16:39
【问题描述】:

我如何以编程方式获取在 Eclipse 插件的 java 编辑器中有插入符号的当前令牌?例如,如果插入符号放在字符串的一个字符上(例如,“text here”),我想在 eclipse 插件中获取“text here”作为值。有API吗?我找到了一个示例来获得突出显示的选择,但找不到仅通过插入符号的偏移量来计算当前标记的示例。

【问题讨论】:

    标签: eclipse-plugin editor token


    【解决方案1】:

    你可以使用

    IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    

    获取当前活动的编辑器。

    然后做(在检查 editorpart 是 ITextEditor 之后)

    IDocumentProvider provider = ((ITextEditor) editor).getDocumentProvider();
    IDocument document = provider.getDocument(((ITextEditor) editor).getEditorInput());
    ITextSelection textSelection = (ITextSelection) ((ITextEditor) editor).getSite().getSelectionProvider().getSelection();
    
    int offset = textSelection.getOffset();
    int lineNumber = document.getLineOfOffset(offset);
    

    获取偏移量和行号,您可以使用此信息从文档中解析出相关的文本/标记。即使没有选择文本(即插入符号位置),这也将起作用。

    【讨论】:

      猜你喜欢
      • 2020-10-29
      • 2019-09-12
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 2017-02-23
      相关资源
      最近更新 更多