Get correct selection with VSCode extension when user navigates to new document - visual-studio-code

I have an extension that subscribes to window.onDidChangeActiveTextEditor. If Go to definition is used, the TextEditor received by onDidChangeActiveTextEditor will report a selection of 0,0. The "correct" selection is transported in a separate event (onDidChangeTextEditorSelection).
The onDidChangeActiveTextEditor event seems to originate from textFileEditors.ts:
textEditor.setModel(textFileModel.textEditorModel);
The selection is updated shortly afterwards:
// Apply options to editor if any
if (options) {
applyTextEditorOptions(options, textEditor, ScrollType.Immediate);
}
Even if flipped around, this has no impact, because the new selection is transported in an internal event that will not reach the code before the onDidChangeActiveTextEditor event is sent to the extension host.
Is there a way for an extension to know if the new editor is opened at 0,0 or another position without waiting to see if the selection is updated shortly after?
The events for opening a editor at (0,0) are:
didOpenTextDocument
didChangeActiveTextEditor
The events for opening an editor at any other position are:
didOpenTextDocument
didChangeActiveTextEditor (position is (0,0))
onDidChangeTextEditorSelection (position is the "correct" position)

Related

Prevent Unity GameObject losing focus when Shift-Clicking to add new point in Custom Editor

I am writing a Custom Editor in Unity - at present I can just drag some points around and the editor draws a line between them all.
I have implemented pressing Shift-Click to add a new point, and this works fine - except...
If I press shift, then click , my new point appears.
If I release shift, then release the mouse button, all is well.
If I release the mouse button first though, the game object that was highlighted (and contains all of my points) loses focus - nothing in the hierarchy has focus - so I lose my handles and the editor is no longer active.
Some of the relevant code
if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.shift)
{
var pos = GetMousePositionOnXYPlane(mouseRay);
var cp = new ControlPoint(ControlPointType.Flat);
cp.Positions.Add(pos);
level.AddControlPoint(cp);
}
I have tried adding
Selection.activeGameObject = _level.gameObject;
immediately after the adding of the control point, to no avail.
If I use Shift / right-click focus is not lost
If I use just Left Click, focus is not lost.
I'm at a loss what to try next...

Event.add for keypress does not work?

We are trying to use tinyMCE for one of our applications. We would like to attach some event handling to particular elements within tinyMCE. We've tried doing it using
Event.add(element, 'keypress', getTag);
Where element is the HTML element we are trying to attach the event to and getTag is the function we'd like called when the keypress event is fired. The particular element we are trying to attach to is a span element with some text in it. We'd like to capture when particular key combinations are entered(like ctrl - F10) between the span tags and popup a menu with options.
The options in the menu will vary depending on the particular span elements the combination is entered in. That's why we want to attach to particular elements instead of globally attaching to all span elements in the document(within tinyMCE). i.e The getTag function will behave differently, using closures, depending on where the combinations are entered.
The problem is when we attach to the particular elements and test them nothing happens for any 'keypress' events. If we try to attach to the span elements using a 'click' event everything works as expected. Once we revert back to using 'keypress' nothing happens again.
Using the debugging tools I've verified a couple of things. The event listeners are attached to the elements. It seems tinyMCE creates a toplevel keypress and click(along with others) to the document within tinyMCE. I'm guessing this is how Editor.onKeyPress().add() like functions work. When I debug the working scenorio using click I can see where the event is fired for both the document and span elements. While debugging the keypress event I only see the event fired for the document element, not the span element. I'm thinking that tinyMCE is suppressing the event, but I can't figure out how, and what needs to be done to stop it.
Use this handler eigther in one of you own plugins or using the tinymce config param setup
ed.onKeyDown.add(function onkeydown(ed, evt) {
var is_in_span = ed.selection.getNode().nodeName == 'SPAN';
// check for caret in SPAN and F10-Key
if (is_in_span && evt.keyCode == '121'){ // you may add other keyCodes here
// do whatever you like here
...
}
});

Get notified when cursor position changed in Eclipse TextEditor

I am developing a plugin to eclipse and I want to add some actions to the context menu. But actually I wanted to prepare results beforehead according to the text selection in the editor and just show them when the menu item will be selected.
I followed that article http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html - all interfaces (ISelectionListener, ISelectionChangedListener etc) allow to handle the SelectionChanged event, but editor counts changing only when length of selection also changes - so the simple click in the editor doesn't fire the event, although I want to get the word (for example) as a selection if cursor is inside the word now and lenght is 0.
So the question is - what is the simpliest solution for traking down cursor position/offset/selections with zero lengh value changing?
In that case you have to use KeyListener and MouseListener as well. For e.g take a look at org.eclipse.jface.text.PaintManager, and it listens to all these events.
If you are extending TextEditor you can override handleCursorPositionChanged() method to fire your event and use getCursorPosition() to get the cursor position as a String.

Change the order of event handling in GWT

I have a Suggest box which has 2 hadlers: SelectionHandler for selecting items in SuggestionList and keyDownHandler on TextBox of SuggestBox. I want to prevent default action on event(for example on Enter pressed) when the suggestion list is currently showing. The problem is that SelectionEvent is always fires before KeyDownEvent and suggestion list is closed after SuggestionEvent fired, so in KeyDownEventHandler suggestion list is already closed. And I can't use prevent default action on Enter with checking the suggestion list is showing like this:
if ((nativeCode == KeyCodes.KEY_TAB || nativeCode == KeyCodes.KEY_ENTER) && display.isSuggestionListShowing()) {
event.preventDefault();
}
where display.isSuggestionListShowing() is the method which calls isShowing on SuggestBox .
So how can i change the order of event handling(Selection before KeyDown to the keyDown before Selection) in this case?
I'm assuming you mean SuggestBox instead of SuggestionList, as there is no class by that name in the gwt-user jar.
The SuggestBox uses the keydown event to provide the SelectEvent - if it can't see the keys change (from the browser, which actually picks up the user's action), it can't provide the logical selection event.
This means that reordering events doesn't really make sense - you can't have the effect before the cause. In many cases, the browser emits events in a certain order, and there is no way to change this, so you have to think differently about the problem.
(Also worth pointing out that preventDefault() only prevents the browser from doing its default behavior - other handlers will still fire as normal.)
One option would be to preview all events before they get to the SuggestBox, and cancel the event in certain cases - look into com.google.gwt.user.client.Event.addNativePreviewHandler(NativePreviewHandler) for how this can be done.
I'm not seeing any other option right away - all of the actual logic for handling the keydown is wrapped up in the inner class in the private method of com.google.gwt.user.client.ui.SuggestBox.addEventsToTextBox(), leaving no options for overriding it.

SWT: Position a dialog based upon the position of the caret in a StyledText control

I have SWT application that is a set of Groups that contain various controls including a StlyedText widget. They are all laid out using the Form layout.
I want to show a dialog directly below the caret inside of the StyledText. However, I have to position the dialog relative to the parent shell.
My first idea is to get the position of the shell plus the position of the StyledText plus the offset of the caret. When I try to get the position of the StyledText, it says 0,0 (I assume because of my layout choice, the Form layout). I don't see a good way to get the position from the FormData either (it appears to be computed).
I am able to get the position of the mouse cursor, but I would like to have it be right under what the user is typing.
Anyone have any ideas?
In order to get the actual postion, the function toDisplay() should be used. For example:
Point displayPoint = myText.toDisplay(sqlText.getLocation());
That gets me to the position of the Text. I then added the caret position in order to move my dialog window to the line of text that is being written:
Point caretLocation = myText.getCaret().getLocation();
Point calcPoint = new Point(displayPoint.x+caretLocation.x, displayPoint.y+caretLocation.y);
I then used that location to position my dialog window.