Skip to content

Commit

Permalink
get token scopes method added
Browse files Browse the repository at this point in the history
  • Loading branch information
NickRimmer committed Mar 29, 2024
1 parent adbe72b commit 62b8790
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/AvaloniaEdit.Demo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ public MainWindow()

private void Caret_PositionChanged(object sender, EventArgs e)
{
_statusTextBlock.Text = string.Format("Line {0} Column {1}",
var scopes = _textMateInstallation.EditorModel.GetTokenScopes(
_textEditor.TextArea.Caret.Line - 1,
_textEditor.TextArea.Caret.Column - 1);

_statusTextBlock.Text = string.Format("Line {0} Column {1}. Scopes: {2}",
_textEditor.TextArea.Caret.Line,
_textEditor.TextArea.Caret.Column);
_textEditor.TextArea.Caret.Column,
scopes.Count > 0 ? string.Join(", ", scopes) : "None");
}

protected override void OnClosed(EventArgs e)
Expand Down
18 changes: 15 additions & 3 deletions src/AvaloniaEdit.TextMate/TextEditorModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;

using System.Collections.Generic;
using System.Linq;
using Avalonia.Threading;

using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;

using TextMateSharp.Model;

namespace AvaloniaEdit.TextMate
Expand Down Expand Up @@ -73,6 +72,19 @@ public override int GetLineLength(int lineIndex)
return _documentSnapshot.GetLineLength(lineIndex);
}

public IReadOnlyCollection<string> GetTokenScopes(int lineIndex, int columnIndex)
{
var line = Get(lineIndex);
if (line == null) return Array.Empty<string>();

var element = line
.Tokens
.OrderBy(x => x.StartIndex)
.LastOrDefault(x => x.StartIndex <= columnIndex);

return element?.Scopes ?? new List<string>();
}

private void TextView_ScrollOffsetChanged(object sender, EventArgs e)
{
try
Expand Down

0 comments on commit 62b8790

Please sign in to comment.