Skip to content

Commit

Permalink
Minibuffer support fix
Browse files Browse the repository at this point in the history
Different editor types require individual buffer adds
  • Loading branch information
mfeber committed Apr 19, 2020
1 parent 8b9c0df commit bc48111
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 52 deletions.
33 changes: 8 additions & 25 deletions Emacs+/src/com/mulgasoft/emacsplus/ModeLineFlasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
package com.mulgasoft.emacsplus;

import static com.mulgasoft.emacsplus.minibuffer.WithMinibuffer.MINIBUFF_ID;
import static com.mulgasoft.emacsplus.minibuffer.WithMinibuffer.POSITION_ID;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.StatusLineLayoutData;
import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.swt.SWT;
Expand All @@ -32,7 +30,7 @@
*
* @author mfeber
*/
public class ModeLineFlasher {
public class ModeLineFlasher extends StatusItemSupport {

private static ModeLineFlasher instance = null;
private ModeLineFlasher() {};
Expand All @@ -49,14 +47,14 @@ public static ModeLineFlasher getInstance() {
return instance;
}

private static final String FLASH_ID = "flash_mode"; // $NON-NLS-1$
private static final String FLASH_ID = "flash_mode"; //$NON-NLS-1$

private static FlashLineContributionItem flashItem = null;
private static int flashCount = 3;
private static int waitTime = 25;

private static String foregroundKey = "org.eclipse.ui.editors.foregroundColor"; // $NON-NLS-1$
private static String backgroundKey = "org.eclipse.ui.editors.backgroundColor"; // $NON-NLS-1$
private static String foregroundKey = "org.eclipse.ui.editors.foregroundColor"; //$NON-NLS-1$
private static String backgroundKey = "org.eclipse.ui.editors.backgroundColor"; //$NON-NLS-1$

private static Color[] backs = { PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_RED), PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_BLACK)};

Expand Down Expand Up @@ -87,29 +85,15 @@ private void clearIt(IContributionItem item, ITextEditor editor) {
asyncForce(editor);
}

private void initFlashItem(ITextEditor editor) {
protected StatusLineContributionItem initStatusLineItem() {
if (flashItem == null) {
setColors();
flashItem = new FlashLineContributionItem(FLASH_ID, true, 83);;
}
flashItem.setBackground(backs[flashCount % backs.length]);
return flashItem;
}

private synchronized void addStatusContribution(ITextEditor editor) {
initFlashItem(editor);
IStatusLineManager slm = EmacsPlusUtils.getStatusLineManager(editor);
IContributionItem fItem = slm.find(FLASH_ID);
if (fItem == null) {
try {
slm.insertBefore(((slm.find(MINIBUFF_ID) != null) ? MINIBUFF_ID : POSITION_ID), flashItem);
} catch (IllegalArgumentException e) {
slm.insertBefore(POSITION_ID, flashItem);
}
} else {
flashItem.setVisible(true);
}
}

/**
* Flash an area on the mode line a number of times
*
Expand All @@ -120,7 +104,7 @@ private synchronized void addStatusContribution(ITextEditor editor) {
*/
public void flashModeLine(Display display) {
final ITextEditor editor = EmacsPlusUtils.getCurrentEditor();
addStatusContribution(editor);
addStatusContribution(editor, MINIBUFF_ID);
asyncForce(editor);
Display.getDefault().asyncExec(() -> {
runFlash(flashCount, flashItem, editor);
Expand Down Expand Up @@ -148,7 +132,7 @@ private void runFlash(final int count, FlashLineContributionItem item, final ITe
/**
* Add a background to a simplified StatusLineContributionItem
*/
private static class FlashLineContributionItem extends StatusLineContributionItem {
private class FlashLineContributionItem extends StatusLineContributionItem {

private static final int INDENT= 3;
private int fFixedWidth= -1;
Expand All @@ -170,7 +154,6 @@ private static class FlashLineContributionItem extends StatusLineContributionIte
*/
public FlashLineContributionItem(String id, boolean visible, int widthInChars) {
super(id);
setVisible(visible);
fWidthInChars= widthInChars;
}

Expand Down
74 changes: 74 additions & 0 deletions Emacs+/src/com/mulgasoft/emacsplus/StatusItemSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright (c) 2009-2020 Mark Feber, MulgaSoft
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package com.mulgasoft.emacsplus;

import static org.eclipse.ui.texteditor.ITextEditorActionConstants.STATUS_CATEGORY_ELEMENT_STATE;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.StatusLineContributionItem;

/**
* Workaround for multiple status line contribution managers
*
* @author mfeber
*/
public abstract class StatusItemSupport {

protected static String POSITION_ID = STATUS_CATEGORY_ELEMENT_STATE;

protected abstract StatusLineContributionItem initStatusLineItem();

/**
* Each editor type adds their own org.eclipse.jface.action.SubContributionManager with a parallel
* set of items to the other editors. We need to add ourselves to each set as we encounter them.
* Eclipse uses the exact same initialization code to modify the EditAction menu, while duplicating
* the StausLineManager setup (in text editors afaik).
*
* @param editor - the current editor
* @param placeId - add the status line item before this
*/
protected synchronized void addStatusContribution(ITextEditor editor, String placeId) {
IStatusLineManager slm = EmacsPlusUtils.getStatusLineManager(editor);
IContributionItem item = initStatusLineItem();
IContributionItem fItem = slm.find(item.getId());
if (fItem == null || !hasInnerItem(slm, item)) {
System.out.println("Adding " + item.getId() + " at " + placeId + " for " + editor.toString());
if (slm.find(placeId) != null) {
slm.insertBefore(placeId, item);
} else {
slm.add(item);
}
}
item.setVisible(true);
}

/**
* We have to paw through the new SubContributionManager's items to see if we've already been
* added. Fortunately, the list only has a few items.
*
* @param slm - the SubContributionManager
* @param item - our statusLine item
* @return true - if we're already present
*/
private boolean hasInnerItem(IStatusLineManager slm, IContributionItem item) {
boolean result = false;
String id = item.getId();
// get the most local items
IContributionItem[] items = slm.getItems();
for (IContributionItem i : items) {
if (id.equals(i.getId())) {
result = true;
break;
}
}
return result;
}
}
41 changes: 14 additions & 27 deletions Emacs+/src/com/mulgasoft/emacsplus/minibuffer/WithMinibuffer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2009, 2013 Mark Feber, MulgaSoft
* Copyright (c) 2009, 2020 Mark Feber, MulgaSoft
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -17,8 +17,6 @@
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.bindings.Binding;
import org.eclipse.jface.bindings.keys.KeySequence;
import org.eclipse.jface.bindings.keys.KeyStroke;
Expand Down Expand Up @@ -55,6 +53,7 @@

import com.mulgasoft.emacsplus.EmacsPlusUtils;
import com.mulgasoft.emacsplus.MarkUtils;
import com.mulgasoft.emacsplus.StatusItemSupport;
import com.mulgasoft.emacsplus.execute.KbdMacroSupport;

/**
Expand All @@ -64,14 +63,12 @@
*
* @author Mark Feber - initial API and implementation
*/
public abstract class WithMinibuffer implements FocusListener, ISelectionChangedListener, ITextListener, MouseListener, VerifyKeyListener {
public abstract class WithMinibuffer extends StatusItemSupport implements FocusListener, ISelectionChangedListener, ITextListener, MouseListener, VerifyKeyListener {

protected final static String EMPTY_STR = ""; //$NON-NLS-1$

// The element before which to insert our status updates
public static final String POSITION_ID = "ElementState"; //$NON-NLS-1$
// The identifier for the StatusLineContributionItem
public static final String MINIBUFF_ID = "minibuffer"; //$NON-NLS-1$
public static final String MINIBUFF_ID = "minibuffer"; //$NON-NLS-1$

static final String N_GEN = "\\c"; //$NON-NLS-1$
static final String N_NEW = "\\n"; //$NON-NLS-1$
Expand Down Expand Up @@ -290,7 +287,6 @@ private boolean install() {
widget = null;
return false;
}
addStatusContribution(editor);
widget.addMouseListener(this);
widget.addFocusListener(this);
viewer.addTextListener(this);
Expand All @@ -309,6 +305,7 @@ private boolean install() {
installed = true;
}
}
addStatusContribution(editor, POSITION_ID);
return installed;
}

Expand Down Expand Up @@ -483,27 +480,17 @@ private synchronized void removeStatusContribution(IWorkbenchPart part) {
EmacsPlusUtils.forceStatusUpdate(part);
}

private synchronized void addStatusContribution(IWorkbenchPart editor) {
miniBuffItem = getStatusLineItem();
IStatusLineManager slm = EmacsPlusUtils.getStatusLineManager(editor);
IContributionItem present = slm.find(MINIBUFF_ID);
if (present == null) {
if (slm.find(POSITION_ID) != null) {
slm.insertBefore(POSITION_ID, miniBuffItem);
} else {
slm.add(miniBuffItem);
}
}
miniBuffItem.setVisible(true);
protected synchronized void addStatusContribution(ITextEditor editor, String placeId) {
super.addStatusContribution(editor, placeId);
miniBuffItem.setText(EMPTY_STR);
}

private synchronized StatusLineContributionItem getStatusLineItem() {
if (miniBuffItem == null) {
miniBuffItem = new StatusLineContributionItem(MINIBUFF_ID, true, getStatusLineLength());
}
return miniBuffItem;
}
protected StatusLineContributionItem initStatusLineItem() {
if (miniBuffItem == null) {
miniBuffItem = new StatusLineContributionItem(MINIBUFF_ID, true, getStatusLineLength());
}
return miniBuffItem;
}

// TODO: compute a reasonable length
protected int getStatusLineLength() {
Expand Down

0 comments on commit bc48111

Please sign in to comment.