protected void updateCaret() {
   if (commandLineText.getCaretOffset() == commandLineText.getCharCount()) {
     commandLineText.setCaret(endCharCaret);
   } else {
     commandLineText.setCaret(defaultCaret);
   }
 }
 @Override
 public void resetContents(String newContents) {
   commandLineText.replaceTextRange(
       contentsOffset, commandLineText.getCharCount() - contentsOffset, newContents);
   // Clear selection, set caret position to end of widget.
   commandLineText.setCaret(endCharCaret);
   commandLineText.setSelection(commandLineText.getCharCount());
 }
  public EclipseCommandLineUI(final StyledText commandLineText, final EditorAdaptor editorAdaptor) {
    clipboard =
        editorAdaptor.getRegisterManager().getRegister(RegisterManager.REGISTER_NAME_CLIPBOARD);
    this.commandLineText = commandLineText;
    commandLineText.addCaretListener(this);
    commandLineText.addSelectionListener(this);

    this.defaultCaret = commandLineText.getCaret();
    this.endCharCaret = CaretUtils.createCaret(CaretType.RECTANGULAR, commandLineText);
    commandLineText.setCaret(endCharCaret);

    contextMenu = new Menu(commandLineText);
    cutItem = new MenuItem(contextMenu, SWT.DEFAULT);
    cutItem.setText("Cut");
    cutItem.setEnabled(false);
    copyItem = new MenuItem(contextMenu, SWT.DEFAULT);
    copyItem.setText("Copy\tCtrl-Y  ");
    copyItem.setEnabled(false);
    pasteItem = new MenuItem(contextMenu, SWT.DEFAULT);
    pasteItem.setText("Paste\tCtrl-R +");
    selectAllItem = new MenuItem(contextMenu, SWT.DEFAULT);
    selectAllItem.setText("Select All");

    commandLineText.setMenu(contextMenu);

    cutItem.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            copySelectionToClipboard();
            erase();
          }
        });
    copyItem.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            copySelectionToClipboard();
          }
        });
    pasteItem.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            RegisterContent content = clipboard.getContent();
            if (content.getPayloadType() == ContentType.TEXT
                || content.getPayloadType() == ContentType.LINES) {
              String text = content.getText();
              text = VimUtils.replaceNewLines(text, " ");
              type(text);
            }
          }
        });
    selectAllItem.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            commandLineText.setSelection(contentsOffset, commandLineText.getCharCount());
            // Manually trigger selection listener because for some reason the method call above
            // doesn't.
            Event e2 = new Event();
            e2.widget = commandLineText;
            e2.x = contentsOffset;
            e2.y = commandLineText.getCharCount();
            EclipseCommandLineUI.this.widgetSelected(new SelectionEvent(e2));
          }
        });
  }