简体   繁体   English

Eclipse RCP:自定义控制台

[英]Eclipse RCP: Custom console

I am trying to create a console that would work as a shell for a custom programming language. 我正在尝试创建一个控制台,作为自定义编程语言的shell。 It would be very similar to the pydev interactive console. 它与pydev交互式控制台非常相似。

Currently, my RCP uses the basic TextConsole and is connected to the shell via pipes so it just displays whatever the shell displays and if the user enters anything in the RCP console, the same is written in the shell. 目前,我的RCP使用基本的TextConsole并通过管道连接到shell,因此它只显示shell显示的内容,如果用户在RCP控制台中输入任何内容,则会在shell中写入相同内容。

I want to be able to do a bit more such as move the caret position, add events for up and down arrow keys etc. I believe to do that I need to add a StyledText widget to the console which is done via the ConsoleViewer. 我希望能够做更多的事情,例如移动插入位置,添加向上和向下箭头键等事件。我相信这样做我需要将一个StyledText小部件添加到控制台,这是通过ConsoleViewer完成的。

So my question is, that is there any way for me to either override the TextConsole's ConsoleViewer or if I were to extend TextConsole and create my own, then how do I link it with the launch configuration (the one that connects the shell via pipes)? 所以我的问题是,有没有办法让我覆盖TextConsole的ConsoleViewer,或者如果我要扩展TextConsole并创建我自己的,那么我如何将它与启动配置(通过管道连接shell的那个)链接起来?

Also, to get the current default console I use DebugUITools.getConsole(process) . 另外,要获取当前的默认控制台,我使用DebugUITools.getConsole(process)

I'm sorry if I haven't put all the information needed; 如果我没有提供所需的所有信息,我很抱歉; it is a bit difficult to explain. 这有点难以解释。 I am happy to add more information. 我很乐意添加更多信息。

An idea... From what I understand I can create a TextConsolePage from the TextConsole using createPage(ConsoleView) . 一个想法......根据我的理解,我可以使用createPage(ConsoleView) TextConsolePageTextConsole创建一个TextConsolePage Once I have the page I can set the viewer via setViewer(viewer) . 有了页面后,我可以通过setViewer(viewer)设置查看setViewer(viewer) Here I thought if I create my own viewer (which will have the appropriate stylewidget) then that could be a lead. 在这里,我想如果我创建自己的查看器(它将具有适当的样式组件),那么这可能是一个领先者。 The only problem is that the viewer needs a Composite and I can't seem to figure out where to get that from. 唯一的问题是观众需要一个复合材料,我似乎无法弄清楚从哪里得到它。

So I thought I would answer this myself as I was finally able to accomplish the console. 所以我想我会自己回答,因为我终于能够完成控制台了。 It still is a working prototype but I guess as you keep adding things, you can clean up the code more and more. 它仍然是一个工作原型,但我想,随着你不断添加东西,你可以越来越多地清理代码。 For my current purposes this is how it worked. 对于我目前的目的,这是它的工作方式。

If you want the short version, then I basically mimicked the ProcessConsole provided by Eclipse as that is what I needed: a console in which I can connect a process but since the ProcessConsole is internal, I like to avoid extending those classes. 如果你想要短版本,那么我基本上模仿了Eclipse提供的ProcessConsole ,因为这就是我所需要的:一个控制台,我可以在其中连接一个进程,但由于ProcessConsole是内部的,我想避免扩展这些类。

Following is an outline of the classes I used to achieve interaction with my console. 以下是我用来与我的控制台进行交互的类的概述。 I am not going to give the pretext as to where MyConsole was created. 我不会以MyConsole创建位置为借口。 Basically, instead of using DebugUITools.getConsole(myProcess) , I used my own myProcess.getConsole() method. 基本上,我使用自己的myProcess.getConsole()方法而不是使用DebugUITools.getConsole(myProcess) MyProcess extends RuntimeProcess . MyProcess扩展了RuntimeProcess

class MyConsole extends IOConsole {
 private IOConsoleInputStream fInput;
 private IOConsoleOutputStream fOutput;
 private IStreamsProxy fStreamsProxy;
 private ConsoleHistory history;
 //This is to remember the caret position after the prompt 
 private int caretAtPrompt;
     /* in the console so when you need to replace the command on up and down 
      * arrow keys you have the position. 
      * I just did a caretAtPrompt += String.Length wherever string was 
      * appended to the console. Mainly in the streamlistener and 
      * InputJob unless you specifically output something to the output 
      * stream.
      */
 //In the constructor you assign all the above fields. Below are some 
 //to point out.
 //fInput = getInputStream();
 // fStreamsProxy = process.getStreamsProxy();
 // fOutput = newOutputStream();

 //We must override the following method to get access to the caret
 @Override
 public IPageBookViewPage createPage(IConsoleView view) {
    return new MyConsolePage(this, view);
    }
 //After this I followed the ProcessConsole and added the 
 //InputJob and StreamListener
 //defined in there. 
 }

class MyConsolePage extends TextConsolePage {
 //Not much in this class, just override the createViewer
 // to return MyConsoleViewer
 }

class MyConsoleViewer extends TextConsoleViewer {
 //This is the most important class and most of the work is done here
 //Again I basically copied everything from IOConsoleViewer and then
 //updated whatever I needed
 //I added a VerifyKeyListener for the up and down arrow 
 //keys for the console history

 MyConsoleViewer (Composite parent, MyConsole console) {
  //I have omitted a lot of code as it was too much to put up, 
  //just highlighted a few
  getTextWidget().addVerifyKeyListener(new MyKeyChecker());
  }

 class MyKeyChecker implements VerifyKeyListener {...}

 }

This is the code for ProcessConsole . ProcessConsole的代码。 This is the code for IOConsoleViewer . IOConsoleViewer的代码。

The ConsoleHistory class I created just had a doubly linked string list to save all the commands the user entered. 我创建的ConsoleHistory类只有一个双向链接的字符串列表,用于保存用户输入的所有命令。 Quite a simple class to create. 相当简单的一个类来创建。

Once you look at the Eclipse classes ( ProcessConsole and IOConsoleViewer ) it is actually all quite self explanatory. 一旦你看了Eclipse类( ProcessConsoleIOConsoleViewer ),它实际上都是非常自我解释的。 I haven't put in much code here because there is quite a bit. 我没有在这里输入太多代码,因为有很多。 But hopefully this gives some direction as I was completely lost when I started. 但希望这会给我一些方向,因为当我开始时我完全失去了。

I am happy to answer questions though and add more specific code if anyone asks. 我很乐意回答问题,如果有人问,请添加更具体的代码。

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

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