Skip to content

Commit

Permalink
RW - use net472 and caching for much faster work.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed May 18, 2022
1 parent 59b725b commit 8fd4570
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 17 deletions.
68 changes: 68 additions & 0 deletions RightWords/Data/Test-1.restext
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
= SkipPattern works =

zz words with digits: 8zz zz zz8zz zz zz8 zz
zz "zz:\zz\zz zz" zz "zz:/zz/zz zz" zz
zz "Z:\zz\zz zz" zz "Z:/zz/zz zz" zz
zz ".\zz\zz zz" zz "./zz/zz zz" zz
zz "\zz\zz zz" zz "/zz/zz zz" zz
zz zz:\zz\zz zz zz:/zz/zz zz
zz Z:\zz\zz zz Z:/zz/zz zz
zz .\zz\zz zz ./zz/zz zz
zz \zz\zz zz /zz/zz zz

= SkipPattern does not work =

_zz_zz_

= Misc =

12345 головомошка favorit
!"£$% голmove favorit
ежик ёжик

_rightRigth_ _rigthRight_ _правильноПравилно_ _правилноПравильно_
IRightRigth IRigthRight ПравильноПравилно ПравилноПравильно
rightRigth rigthRight правильноПравилно правилноПравильно

= With ampersand =

Ampe&rsand

= Long line =

головомошка favorit 1 головомошка favorit 2 головомошка favorit 3 головомошка favorit 4 головомошка favorit 5

= Уже в словарях =

nightroman
Кузьмин

* plugin ~ pin
* plugin plugins Plugin Plugins

* плагин ~ камин
* ед.число мн.число заглавные
И плагин плагины Плагин Плагины
Р плагина плагинов Плагина Плагинов
Д плагину плагинам Плагину Плагинам
В плагин плагины Плагин Плагины
Т плагином плагинами Плагином Плагинами
П плагине плагинах Плагине Плагинах

опциональная

+ exabyte petabyte terabyte Exabytes Petabytes Terabytes

+ Artifact Artifacts | Artifacted (fact | act)
+ artifact artifacts | artifacted (fact | act)

+ Catalog Catalogs Catalogged
+ catalog catalogs catalogged

+ Discoverability Discoverabilities
+ discoverability discoverabilities

- dialog dialogs | dialogged

- Color Colors Colored? | Colorred?
- color colors colored? | colorred?
10 changes: 10 additions & 0 deletions RightWords/Data/Test-2.lng
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

"M&an"
"Ma&n"
"Man&"
"&Man"

"M&anz"
"Ma&nz"
"Man&z"
"Manz&"
116 changes: 103 additions & 13 deletions RightWords/Highlighter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,103 @@ public class Highlighter : ModuleDrawer
{
readonly MultiSpell Spell = MultiSpell.Get();
readonly HashSet<string> CommonWords = Actor.GetCommonWords();

/// <summary>
/// Last checked line data cache.
/// </summary>
LineData[] LastData = Array.Empty<LineData>();

/// <summary>
/// Line text and color spans.
/// </summary>
class LineData
{
public string Text;
public ValueTuple<int, int>[] Spans;
}

/// <summary>
/// Finds the cached data by the line hint index and text.
/// </summary>
LineData FindLineData(int index, string text)
{
// if the frame is not changed (arrows without scrolling or typing in the same line)
// then many not changed lines are in the same positions, check the hint index first
if (index < LastData.Length)
{
if (LastData[index]?.Text == text)
return LastData[index];
}

// on scrolling or typing many lines shift positions, find from the hint index
for (int i = 1; ; ++i)
{
int j = index - i;
int k = index + i;
bool ok1 = j >= 0 && j < LastData.Length;
bool ok2 = k >= 0 && k < LastData.Length;
if (!ok1 && !ok2)
break;
if (ok1 && LastData[j]?.Text == text)
return LastData[j];
if (ok2 && LastData[k]?.Text == text)
return LastData[k];
}

return null;
}

public override void Invoke(IEditor editor, ModuleDrawerEventArgs e)
{
var sets = Settings.Default.GetData();
var settings = Settings.Default.GetData();

int topLineIndex = e.Lines[0].Index;
var newData = new LineData[e.Lines.Count];

int newDataIndex = -1;
var lineSpans = new List<ValueTuple<int, int>>();
foreach (var line in e.Lines)
{
++newDataIndex;
var text = line.Text;
if (text.Length == 0)
continue;

if (sets.MaximumLineLength > 0 && text.Length > sets.MaximumLineLength)
// rare case: too long line, color the whole line
if (settings.MaximumLineLength > 0 && text.Length > settings.MaximumLineLength)
{
e.Colors.Add(new EditorColor(
line.Index,
0,
text.Length,
sets.HighlightingForegroundColor,
sets.HighlightingBackgroundColor));
settings.HighlightingForegroundColor,
settings.HighlightingBackgroundColor));
continue;
}

// try get the cached line data
var data = FindLineData(line.Index - topLineIndex, text);
if (data != null)
{
// keep it as new and add colors
newData[newDataIndex] = data;
if (data.Spans != null)
{
foreach (var item in data.Spans)
e.Colors.Add(new EditorColor(
line.Index,
item.Item1,
item.Item2,
settings.HighlightingForegroundColor,
settings.HighlightingBackgroundColor));
}
continue;
}

// parse and check words, collect color spans
lineSpans.Clear();
MatchCollection skip = null;
for (var match = sets.WordRegex2.Match(text); match.Success; match = match.NextMatch())
for (var match = settings.WordRegex2.Match(text); match.Success; match = match.NextMatch())
{
// the target word
var word = Actor.MatchToWord(match);
Expand All @@ -50,18 +124,34 @@ public override void Invoke(IEditor editor, ModuleDrawerEventArgs e)
continue;

// expensive skip pattern
if (Actor.HasMatch(skip ?? (skip = Actor.GetMatches(sets.SkipRegex2, text)), match))
if (Actor.HasMatch(skip ?? (skip = Actor.GetMatches(settings.SkipRegex2, text)), match))
continue;

// add color
e.Colors.Add(new EditorColor(
line.Index,
match.Index,
match.Index + match.Length,
sets.HighlightingForegroundColor,
sets.HighlightingBackgroundColor));
// add the span
lineSpans.Add((match.Index, match.Index + match.Length));
}

// cache the data and add colors if any
if (lineSpans.Count == 0)
{
newData[newDataIndex] = new LineData { Text = text };
}
else
{
newData[newDataIndex] = new LineData { Text = text, Spans = lineSpans.ToArray() };

foreach (var span in lineSpans)
e.Colors.Add(new EditorColor(
line.Index,
span.Item1,
span.Item2,
settings.HighlightingForegroundColor,
settings.HighlightingBackgroundColor));
}
}

// update cache
LastData = newData;
}
}
}
5 changes: 5 additions & 0 deletions RightWords/History.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
https://www.nuget.org/packages/FarNet.RightWords

= 2.5.0 =

Requires net472.
Use caching for much faster processing.

= 2.4.0 =

Requires FarNet 5.6.0.
Expand Down
2 changes: 1 addition & 1 deletion RightWords/LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2011-2021 Roman Kuzmin
Copyright (c) 2011-2022 Roman Kuzmin

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion RightWords/RightWords.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<MyOutDir>$(FarHome)\FarNet\Modules\$(AssemblyName)</MyOutDir>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net45</TargetFramework>
<TargetFramework>net472</TargetFramework>
<RunPostBuildEvent>1</RunPostBuildEvent>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
Expand Down
2 changes: 0 additions & 2 deletions RightWords/TheTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public class TheTool : ModuleTool
{
public override void Invoke(object sender, ModuleToolEventArgs e)
{
if (e == null) return;

var menu = Far.Api.CreateMenu();
menu.Title = Settings.ModuleName;
menu.HelpTopic = Far.Api.GetHelpTopic("main-menu");
Expand Down

0 comments on commit 8fd4570

Please sign in to comment.