Skip to content

Commit

Permalink
Workaround for Scintilla text-wrap bugs
Browse files Browse the repository at this point in the history
Don't open same field multiple times (unless CTRL pressed)
  • Loading branch information
Pedro Fonseca committed Feb 17, 2024
1 parent b88ff0a commit 639a80f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 72 deletions.
99 changes: 58 additions & 41 deletions Zelda/GUI/ExpressionTab.cs
Expand Up @@ -41,6 +41,10 @@ public partial class ExpressionTab : TabPage
internal string linkedField { get; set; }
internal bool isLinkedTab => !string.IsNullOrEmpty(linkedField);

bool wrap;
bool viewEol;
bool delayedWrap = false;

public ExpressionTab(Settings settings)
{
InitializeComponent();
Expand All @@ -58,15 +62,22 @@ public ExpressionTab(string title, Settings settings) : this(settings)
Text = title;
}

public void SetContent(string content)
public void SetContent(string content, int pos = -1)
{
wrap = scintilla.WrapMode != WrapMode.None;
viewEol = scintilla.ViewEol;
delayedWrap = true;
scintilla.WrapMode = WrapMode.None;

savedExpression = content;
scintilla.Text = content;
if (IsHandleCreated)
syntaxHighlight(true);
else
this.HandleCreated += (a, b) => syntaxHighlight(true);

if (pos >=0)
scintilla.GotoPosition(pos);
scintilla.EmptyUndoBuffer();
isSaved = true;

syntaxHighlight(true);
}

public void Config(Settings settings)
Expand Down Expand Up @@ -137,7 +148,6 @@ public void Config(Settings settings)
//scintilla.SetRepresentation("\r", "CR");

runTimer.Interval = settings.EvaluateDelay;
syntaxHighlight();
}

private void scintilla_TextChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -172,41 +182,6 @@ public bool CheckSavedExpression()
return isSaved;
}

// handle caret position change - highlight current function
private void scintilla_UpdateUI(object sender, UpdateUIEventArgs e)
{
if (!textChanged && e.Change == UpdateChange.Selection)
{
HighlightWord(scintilla.SelectedText, scintilla.SelectionStart);
syntaxIndicators();
}
}

private void HighlightWord(string text, int position)
{
// clear all indicator occurences
scintilla.IndicatorCurrent = 3;
scintilla.IndicatorClearRange(0, scintilla.TextLength);

if (string.IsNullOrEmpty(text) || text.Length < 2)
return;

// Search the document
scintilla.TargetStart = 0;
scintilla.TargetEnd = scintilla.TextLength;
scintilla.SearchFlags = SearchFlags.None;
while (scintilla.SearchInTarget(text) != -1)
{
// Mark the search results with the current indicator
if (scintilla.TargetStart != position)
scintilla.IndicatorFillRange(scintilla.TargetStart, scintilla.TargetEnd - scintilla.TargetStart);

// Search the remainder of the document
scintilla.TargetStart = scintilla.TargetEnd;
scintilla.TargetEnd = scintilla.TextLength;
}
}

private void scintilla_ZoomChanged(object sender, EventArgs e)
{
SetZoom(scintilla.Zoom, true);
Expand Down Expand Up @@ -303,6 +278,16 @@ private void syntaxHighlight(bool forced = false)
});
}

// handle caret position change - highlight current function
private void scintilla_UpdateUI(object sender, UpdateUIEventArgs e)
{
if (!textChanged && e.Change == UpdateChange.Selection)
{
HighlightWord(scintilla.SelectedText, scintilla.SelectionStart);
syntaxIndicators();
}
}

private void syntaxHighlight(List<ELToken> tokens, bool forced = false)
{
if (this.InvokeRequired)
Expand Down Expand Up @@ -336,6 +321,13 @@ private void syntaxHighlight(List<ELToken> tokens, bool forced = false)
}

syntaxIndicators();

if (delayedWrap)
{
delayedWrap = false;
scintilla.ViewEol = viewEol;
scintilla.WrapMode = wrap ? WrapMode.Word : WrapMode.None;
}
}

private void syntaxIndicators()
Expand Down Expand Up @@ -376,6 +368,31 @@ private void syntaxIndicators()
}
}

private void HighlightWord(string text, int position)
{
// clear all indicator occurences
scintilla.IndicatorCurrent = 3;
scintilla.IndicatorClearRange(0, scintilla.TextLength);

if (string.IsNullOrEmpty(text) || text.Length < 2)
return;

// Search the document
scintilla.TargetStart = 0;
scintilla.TargetEnd = scintilla.TextLength;
scintilla.SearchFlags = SearchFlags.None;
while (scintilla.SearchInTarget(text) != -1)
{
// Mark the search results with the current indicator
if (scintilla.TargetStart != position)
scintilla.IndicatorFillRange(scintilla.TargetStart, scintilla.TargetEnd - scintilla.TargetStart);

// Search the remainder of the document
scintilla.TargetStart = scintilla.TargetEnd;
scintilla.TargetEnd = scintilla.TextLength;
}
}

#endregion

#region Expression evaluation
Expand Down
5 changes: 3 additions & 2 deletions Zelda/GUI/ZeldaUI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 34 additions & 27 deletions Zelda/GUI/ZeldaUI.cs
Expand Up @@ -78,22 +78,6 @@ private void ZeldaUI_Load(object sender, EventArgs e)

void LoadState()
{
tabsLeft.SuspendLayout();
if (settings.SaveExpressions && state.Tabs.Count > 0)
{
int current = state.CurrentTab;
foreach (var exp in state.Tabs)
{
// recover linked field if State was re-saved by an older version
if (string.IsNullOrEmpty(exp.linkedField) && Regex.IsMatch(exp.name, @"🔗 \[.+\]$"))
exp.linkedField = exp.name.Substring(exp.name.IndexOf('[')+1).TrimEnd(']');
AddExpressionTab(exp.name, exp.content, exp.position, exp.linkedField);
}
tabsLeft.SelectedIndex = current;
}
else
tabsLeft.SelectedTab = AddExpressionTab();

if (settings.SaveState)
{
chkWhitespace.Checked = state.Whitespace;
Expand All @@ -114,6 +98,23 @@ void LoadState()
if (state.SplitPosition > 0)
splitContainer1.SplitterDistance = state.SplitPosition;
}

tabsLeft.SuspendLayout();
if (settings.SaveExpressions && state.Tabs.Count > 0)
{
int current = state.CurrentTab;
foreach (var exp in state.Tabs)
{
// recover linked field if State was re-saved by an older version
if (string.IsNullOrEmpty(exp.linkedField) && Regex.IsMatch(exp.name, @"🔗 \[.+\]$"))
exp.linkedField = exp.name.Substring(exp.name.IndexOf('[') + 1).TrimEnd(']');
AddExpressionTab(exp.name, exp.content, exp.position, exp.linkedField);
}
tabsLeft.SelectedIndex = current;
}
else
tabsLeft.SelectedTab = AddExpressionTab();

resizeGridColumns();
tabsLeft.ResumeLayout();
}
Expand Down Expand Up @@ -444,11 +445,6 @@ private ExpressionTab AddExpressionTab(string name = null, string content = null
tab.FunctionChanged += expression_FunctionChanged;
tab.NeedsSavingChanged += (sender, saved) => { if (((ExpressionTab)sender).isLinkedTab) tabsLeft.Invalidate(); };
tab.linkedField = linkedField;

if (content != null)
tab.SetContent(content);

if (pos >= 0) tab.scintilla.GotoPosition(pos);
tabsLeft.TabPages.Add(tab);

DataTable dt = gridFiles.DataSource as DataTable;
Expand All @@ -460,13 +456,15 @@ private ExpressionTab AddExpressionTab(string name = null, string content = null
reorderDatagridColumns();
}

tabsLeft.SelectedTab = tab;
tab.scintilla.ViewEol = chkWhitespace.Checked;
tab.scintilla.WrapMode = chkWrap.Checked ? WrapMode.Word : WrapMode.None;
tab.scintilla.ViewWhitespace = chkWhitespace.Checked ? WhitespaceMode.VisibleAlways : WhitespaceMode.Invisible;
tab.scintilla.ViewEol = chkWhitespace.Checked;
tab.scintilla.EmptyUndoBuffer();
if (content != null)
tab.SetContent(content, pos);

tab.scintilla.Focus();
tab.Evaluate(currentFile);

return tab;
}

Expand Down Expand Up @@ -737,7 +735,7 @@ private void btnNextFile_Click(object sender, EventArgs e)

private void btnNew_Click(object sender, EventArgs e)
{
AddExpressionTab();
tabsLeft.SelectedTab = AddExpressionTab();
}

private void btnAutorun_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -1152,7 +1150,7 @@ private void btnClose_Click(object sender, EventArgs e)
dt.Columns.Remove(tab.ID);

if (tabsLeft.TabCount == 0)
AddExpressionTab();
tabsLeft.SelectedTab = AddExpressionTab();

reorderDatagridColumns();
}
Expand Down Expand Up @@ -1312,9 +1310,18 @@ private void btnLink_Click(object sender, EventArgs e)

private void menuFieldList_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// switch to tab if it's already open, unless CTRL pressed
if (!ModifierKeys.HasFlag(Keys.Control))
{
var existing = tabsLeft.TabPages.Cast<ExpressionTab>().FirstOrDefault(t => t.linkedField == e.ClickedItem.Text);
if (existing != null)
tabsLeft.SelectedTab = existing;
return;
}

string name = $"🔗 [{e.ClickedItem.Text}]";
string exp = e.ClickedItem.Tag as string;
AddExpressionTab(name, exp, linkedField: e.ClickedItem.Text);
tabsLeft.SelectedTab = AddExpressionTab(name, exp, linkedField: e.ClickedItem.Text);
}

private void btnRevert_Click(object sender, EventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions Zelda/Properties/AssemblyInfo.cs
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.7.1.0")]
[assembly: AssemblyFileVersion("1.7.1.0")]
[assembly: AssemblyVersion("1.7.2.0")]
[assembly: AssemblyFileVersion("1.7.2.0")]

0 comments on commit 639a80f

Please sign in to comment.