【问题标题】:How to implement auto bracket-closing?如何实现自动括号关闭?
【发布时间】:2016-03-28 19:55:43
【问题描述】:

我正在开发一个包含自定义文本编辑器的 Eclipse 插件。
我想实现自动右括号(和引号)的功能,这意味着当用户键入( 时,编辑器应该自动插入相应的右括号)

我目前的方法是在基础IDocument 中添加一个IDocumentListener,每当文档发生更改时,我都会查看新字符,如果它是一个我想添加结束字符的字符,我正在添加它通过设置文档的文本,但这总是抛出我的这个异常:

!SESSION 2015-12-22 15:03:53.517 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.8.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=de_DE
Framework arguments:  -product org.eclipse.sdk.ide
Command-line arguments:  -product org.eclipse.sdk.ide -data C:\Users\Robert Adam\Documents\eclipse.mars.pluginDev\workspace/../runtime-EclipseApplication(1) -dev file:C:/Users/Robert Adam/Documents/eclipse.mars.pluginDev/workspace/.metadata/.plugins/org.eclipse.pde.core/Eclipse Application (1)/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog

!ENTRY org.eclipse.text 4 2 2015-12-22 15:04:23.611
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.text".
!STACK 0
org.eclipse.core.runtime.AssertionFailedException: assertion failed: 
    at org.eclipse.core.runtime.Assert.isTrue(Assert.java:110)
    at org.eclipse.core.runtime.Assert.isTrue(Assert.java:96)
    at org.eclipse.ui.internal.texteditor.quickdiff.DocumentLineDiffer.handleAboutToBeChanged(DocumentLineDiffer.java:816)
    at org.eclipse.ui.internal.texteditor.quickdiff.DocumentLineDiffer.documentAboutToBeChanged(DocumentLineDiffer.java:785)
    at org.eclipse.jface.text.AbstractDocument.fireDocumentAboutToBeChanged(AbstractDocument.java:665)
    at org.eclipse.jface.text.AbstractDocument.set(AbstractDocument.java:1228)
    at org.eclipse.jface.text.AbstractDocument.set(AbstractDocument.java:1217)
    at raven.sqdev.editors.sqfeditor.SQFDocumentListener.handleAddition(SQFDocumentListener.java:61)
    at raven.sqdev.editors.sqfeditor.SQFDocumentListener.documentChanged(SQFDocumentListener.java:33)
    at org.eclipse.jface.text.AbstractDocument.doFireDocumentChanged2(AbstractDocument.java:769)
    at org.eclipse.jface.text.AbstractDocument.doFireDocumentChanged(AbstractDocument.java:736)
    at org.eclipse.jface.text.AbstractDocument.doFireDocumentChanged(AbstractDocument.java:721)
    at org.eclipse.jface.text.AbstractDocument.fireDocumentChanged(AbstractDocument.java:796)
    at org.eclipse.jface.text.AbstractDocument.replace(AbstractDocument.java:1191)
    at org.eclipse.jface.text.AbstractDocument.replace(AbstractDocument.java:1210)
    at org.eclipse.jface.text.DefaultDocumentAdapter.replaceTextRange(DefaultDocumentAdapter.java:248)
    at org.eclipse.swt.custom.StyledText.modifyContent(StyledText.java:7268)
    at org.eclipse.swt.custom.StyledText.sendKeyEvent(StyledText.java:8111)
    at org.eclipse.swt.custom.StyledText.doContent(StyledText.java:2481)
    at org.eclipse.swt.custom.StyledText.handleKey(StyledText.java:5955)
    at org.eclipse.swt.custom.StyledText.handleKeyDown(StyledText.java:5986)
    at org.eclipse.swt.custom.StyledText$7.handleEvent(StyledText.java:5663)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1085)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1070)
    at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1112)
    at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1108)
    at org.eclipse.swt.widgets.Widget.wmChar(Widget.java:1529)
    at org.eclipse.swt.widgets.Control.WM_CHAR(Control.java:4722)
    at org.eclipse.swt.widgets.Canvas.WM_CHAR(Canvas.java:343)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4610)
    at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:339)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5023)
    at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
    at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2549)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1151)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1032)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:148)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:636)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:579)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:135)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1438)

据我所知,这是因为我正在更改侦听器的文本,这不是一个好主意,因为更改本身会通知侦听器,这可能导致无限循环。
但是我只是想不出一个更好的主意来实现这个功能。

我认为这种任务可能与 ContentAssist 相关,但我找不到任何相关信息。

有人知道如何在我的 Eclipse 插件中正确实现此功能吗?

【问题讨论】:

  • 我会看一下JDT的Java编辑器的源代码,看看它是如何解决这个问题的。
  • 我没有在源代码中找到相应的部分...你能给我一个提示,这个机制可以实现吗?
  • 从阅读来源看来,BracketInserter 的内部类CompilationUnitEditor 负责。它使用VerifyKeyListener 来检测左括号插入。
  • 谢谢!我去看看

标签: java eclipse autocomplete eclipse-plugin jface


【解决方案1】:

查看org.eclipse.jdt.ui 插件的源代码后,我发现他们在编辑器的SourceViewer 上使用VerifyKeyListener 创建了此功能。它捕获所有关键输入并通过事件访问相应的文本。

然而,我的编辑器的SourceViewer 是在我的编辑器之后创建的,因此在我的编辑器的构造函数中调用时,方法getSourceViewer() 返回null
关键是在AbstractDecoratedTextEditor 中声明的编辑器类中覆盖createSourceViewer()
在那里我做了这样的事情:

@Override
    public ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
        ISourceViewer viewer = super.createSourceViewer(parent, ruler, styles);

        if(viewer instanceof ITextViewerExtension) {
            ((ITextViewerExtension) viewer).appendVerifyKeyListener(<Listener>);
        }

        return viewer;
    }

我实现了自己的VerifyKeyListern,在其中我处理查找配对字符(如左括号和右括号),然后在verifyKey()-方法中使用了类似的东西:

StyledText text = (StyledText) event.getSource();

text.insert(String.valueOf(<pairingCharacter>));

由于键入的字符(开始字符)被插入到文本中,我只通过insert()-方法添加相应的结束字符,该方法具有很好的效果,之后我的光标位于这两个字符之间

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2012-06-18
    • 2012-12-02
    • 2010-11-04
    相关资源
    最近更新 更多