C
C#2y ago
Notro

Get word at mouse function doesn't work as I want

Hi. I have this code:
private string GetWordAtMousePosition(MouseEventArgs e)
{
var mousePosition = GetTextEditor().GetPositionFromPoint(e.GetPosition(GetTextEditor()));

if (mousePosition == null)
return string.Empty;

var line = mousePosition.Value.Line;
var column = mousePosition.Value.Column;
var offset = GetTextEditor().Document.GetOffset(line, column);

if (offset >= GetTextEditor().Document.TextLength)
offset--;

int offsetStart = TextUtilities.GetNextCaretPosition(GetTextEditor().Document, offset, LogicalDirection.Backward, CaretPositioningMode.WordBorder);
int offsetEnd = TextUtilities.GetNextCaretPosition(GetTextEditor().Document, offset, LogicalDirection.Forward, CaretPositioningMode.WordBorder);

if (offsetEnd == -1 || offsetStart == -1)
return string.Empty;

var currentChar = GetTextEditor().Document.GetText(offset, 1);

if (string.IsNullOrWhiteSpace(currentChar))
return string.Empty;

return GetTextEditor().Document.GetText(offsetStart, offsetEnd - offsetStart);
}
private string GetWordAtMousePosition(MouseEventArgs e)
{
var mousePosition = GetTextEditor().GetPositionFromPoint(e.GetPosition(GetTextEditor()));

if (mousePosition == null)
return string.Empty;

var line = mousePosition.Value.Line;
var column = mousePosition.Value.Column;
var offset = GetTextEditor().Document.GetOffset(line, column);

if (offset >= GetTextEditor().Document.TextLength)
offset--;

int offsetStart = TextUtilities.GetNextCaretPosition(GetTextEditor().Document, offset, LogicalDirection.Backward, CaretPositioningMode.WordBorder);
int offsetEnd = TextUtilities.GetNextCaretPosition(GetTextEditor().Document, offset, LogicalDirection.Forward, CaretPositioningMode.WordBorder);

if (offsetEnd == -1 || offsetStart == -1)
return string.Empty;

var currentChar = GetTextEditor().Document.GetText(offset, 1);

if (string.IsNullOrWhiteSpace(currentChar))
return string.Empty;

return GetTextEditor().Document.GetText(offsetStart, offsetEnd - offsetStart);
}
The problem is, when I hover on "variable" in this: {variable.test} it gives me "variable", but I want it to return me {variable.test}. Can you help me, or is it AvalonEdit problem?
5 Replies
Anchy
Anchy2y ago
I dont know anything about AvalonEdit but what happens when you use CaretPositioningMode.WordBorderOrSymbol
Notro
Notro2y ago
Hi, thanks for reply, discord wasn't sending me notifications. Good idea, but it isn't working
string word = GetWordAtMousePosition(e);

toolTip.PlacementTarget = sender as UIElement;
toolTip.Content = word;
toolTip.IsOpen = true;
e.Handled = true;
string word = GetWordAtMousePosition(e);

toolTip.PlacementTarget = sender as UIElement;
toolTip.Content = word;
toolTip.IsOpen = true;
e.Handled = true;
Notro
Notro2y ago
Notro
Notro2y ago
Notro
Notro2y ago