© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
6 replies
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}
{variable.test}
it gives me "variable", but I want it to return me
{variable.test}
{variable.test}
. Can you help me, or is it AvalonEdit problem?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ my mouse doesn’t work
C#CC# / help
3y ago
❔ ✅ Why doesn't this basic HttpClient GET request doesn't work as expected?
C#CC# / help
3y ago
seems like this doesn't work as intended!
C#CC# / help
2y ago
❔ Filter doesn't work
C#CC# / help
3y ago