C#C
C#4y 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);
}

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?
Was this page helpful?