Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.net 3.5 and .net core 3.1 targets #211

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ TestResults
## files generated by popular Visual Studio add-ons.

# User-specific files
.vs/
*.suo
*.user
*.sln.docstates
Expand Down Expand Up @@ -106,3 +107,4 @@ Generated_Code #added for RIA/Silverlight projects
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
/mytest
11 changes: 8 additions & 3 deletions FastColoredTextBox.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30320.27
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tester", "Tester\Tester.csproj", "{EBE443EE-F4C7-49E6-AC22-959CA62FAA05}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastColoredTextBox", "FastColoredTextBox\FastColoredTextBox.csproj", "{6DD14A85-CCFC-4774-BD26-0F5772512319}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastColoredTextBox", "FastColoredTextBox\FastColoredTextBox.csproj", "{6DD14A85-CCFC-4774-BD26-0F5772512319}"
EndProject
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "TesterVB", "TesterVB\TesterVB.vbproj", "{3EF3A5A0-2365-41FD-97A0-254A2CFC6577}"
EndProject
Expand Down Expand Up @@ -51,4 +53,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {742C02CF-4566-488A-A923-493BF132C1CA}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion FastColoredTextBox/AutocompleteItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public virtual string GetTextForReplace()
/// </summary>
public virtual CompareResult Compare(string fragmentText)
{
if (Text.StartsWith(fragmentText, StringComparison.InvariantCultureIgnoreCase) &&
if(string.IsNullOrEmpty(fragmentText) || Text.ToLowerInvariant().Contains(fragmentText.ToLowerInvariant()) &&
Text != fragmentText)
return CompareResult.VisibleAndSelected;

Expand Down
30 changes: 25 additions & 5 deletions FastColoredTextBox/AutocompleteMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,43 @@ internal void DoAutocomplete(bool forced)
&& (tb.Selection.Start > fragment.Start || text.Length == 0/*pops up only if caret is after first letter*/)))
{
Menu.Fragment = fragment;
bool foundSelected = false;
//bool foundSelected = false;
AutocompleteItem foundItem = null;
//build popup menu
foreach (var item in sourceItems)
{
item.Parent = Menu;
CompareResult res = item.Compare(text);
if(res != CompareResult.Hidden)
visibleItems.Add(item);
if (res == CompareResult.VisibleAndSelected && !foundSelected)
if (res == CompareResult.VisibleAndSelected && foundItem != null)
{
foundSelected = true;
FocussedItemIndex = visibleItems.Count - 1;
foundItem = item;
//FocussedItemIndex = visibleItems.Count - 1;
}
}

if (foundSelected)
visibleItems.Sort((AutocompleteItem a, AutocompleteItem b)=> {
var indexA = a.Text.IndexOf(text, StringComparison.InvariantCultureIgnoreCase);
var indexB = b.Text.IndexOf(text, StringComparison.InvariantCultureIgnoreCase);

if(indexA == indexB) {
if(a.Text.Length == b.Text.Length) return string.Compare(a.Text, b.Text, true);
return a.Text.Length - b.Text.Length;
}
else {
if(indexA == -1) return 1;
if(indexB == -1) return -1;
}

return indexA - indexB;
});

if (foundItem != null)
{

FocussedItemIndex = visibleItems.IndexOf(foundItem);

AdjustScroll();
DoSelectedVisible();
}
Expand Down
46 changes: 46 additions & 0 deletions FastColoredTextBox/CharSizeCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FastColoredTextBoxNS
{
/// <summary>
/// Calling MeasureText on each char is extremly slow
/// Thankfully the size of the char does not change for the same font
/// However we would still need to indentify the font somehow
/// </summary>
static class CharSizeCache
{
static readonly Dictionary<string, SizeF> cache = new Dictionary<string, SizeF>();
internal static SizeF GetCharSize(Font font, char c)
{
var key = GetKey(font, c);
if (!cache.ContainsKey(key))
{
Size sz2 = TextRenderer.MeasureText("<" + c.ToString() + ">", font);
Size sz3 = TextRenderer.MeasureText("<>", font);
cache[key] = new SizeF(sz2.Width - sz3.Width + 1, /*sz2.Height*/font.Height);
}
return cache[key];
}

/// <summary>
/// Font is disposable, so we need to indentify it without keeping manged resources
/// </summary>
/// <param name="font"></param>
/// <param name="c"></param>
/// <returns></returns>
private static string GetKey(Font font, char c)
{
return font.FontFamily.Name
+ ":" + font.Size
+ ":" + font.Style
+ ":" + font.Unit
+ ":" + font.GdiCharSet
+ ":" + font.GdiVerticalFont
+ ":" + c;
}
}
}
44 changes: 28 additions & 16 deletions FastColoredTextBox/FastColoredTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public FastColoredTextBox()
RightBracket = '\x0';
LeftBracket2 = '\x0';
RightBracket2 = '\x0';
SyntaxHighlighter = new SyntaxHighlighter(this);
language = Language.Custom;
SyntaxHighlighter = SyntaxHighlighter.CreateSyntaxHighlighter(this, language);
PreferredLineWidth = 0;
needRecalc = true;
lastNavigatedDateTime = DateTime.Now;
Expand Down Expand Up @@ -288,7 +288,7 @@ public override bool AllowDrop
get { return base.AllowDrop; }
set { base.AllowDrop = value; }
}

/// <summary>
/// Collection of Hints.
/// This is temporary buffer for currently displayed hints.
Expand Down Expand Up @@ -821,7 +821,7 @@ public Style[] Styles
/// </summary>
[Description("Here you can change hotkeys for FastColoredTextBox.")]
[Editor(typeof(HotkeysEditor), typeof(UITypeEditor))]
[DefaultValue("Tab=IndentIncrease, Escape=ClearHints, PgUp=GoPageUp, PgDn=GoPageDown, End=GoEnd, Home=GoHome, Left=GoLeft, Up=GoUp, Right=GoRight, Down=GoDown, Ins=ReplaceMode, Del=DeleteCharRight, F3=FindNext, Shift+Tab=IndentDecrease, Shift+PgUp=GoPageUpWithSelection, Shift+PgDn=GoPageDownWithSelection, Shift+End=GoEndWithSelection, Shift+Home=GoHomeWithSelection, Shift+Left=GoLeftWithSelection, Shift+Up=GoUpWithSelection, Shift+Right=GoRightWithSelection, Shift+Down=GoDownWithSelection, Shift+Ins=Paste, Shift+Del=Cut, Ctrl+Back=ClearWordLeft, Ctrl+Space=AutocompleteMenu, Ctrl+End=GoLastLine, Ctrl+Home=GoFirstLine, Ctrl+Left=GoWordLeft, Ctrl+Up=ScrollUp, Ctrl+Right=GoWordRight, Ctrl+Down=ScrollDown, Ctrl+Ins=Copy, Ctrl+Del=ClearWordRight, Ctrl+0=ZoomNormal, Ctrl+A=SelectAll, Ctrl+B=BookmarkLine, Ctrl+C=Copy, Ctrl+E=MacroExecute, Ctrl+F=FindDialog, Ctrl+G=GoToDialog, Ctrl+H=ReplaceDialog, Ctrl+I=AutoIndentChars, Ctrl+M=MacroRecord, Ctrl+N=GoNextBookmark, Ctrl+R=Redo, Ctrl+U=UpperCase, Ctrl+V=Paste, Ctrl+X=Cut, Ctrl+Z=Undo, Ctrl+Add=ZoomIn, Ctrl+Subtract=ZoomOut, Ctrl+OemMinus=NavigateBackward, Ctrl+Shift+End=GoLastLineWithSelection, Ctrl+Shift+Home=GoFirstLineWithSelection, Ctrl+Shift+Left=GoWordLeftWithSelection, Ctrl+Shift+Right=GoWordRightWithSelection, Ctrl+Shift+B=UnbookmarkLine, Ctrl+Shift+C=CommentSelected, Ctrl+Shift+N=GoPrevBookmark, Ctrl+Shift+U=LowerCase, Ctrl+Shift+OemMinus=NavigateForward, Alt+Back=Undo, Alt+Up=MoveSelectedLinesUp, Alt+Down=MoveSelectedLinesDown, Alt+F=FindChar, Alt+Shift+Left=GoLeft_ColumnSelectionMode, Alt+Shift+Up=GoUp_ColumnSelectionMode, Alt+Shift+Right=GoRight_ColumnSelectionMode, Alt+Shift+Down=GoDown_ColumnSelectionMode")]
[DefaultValue("Tab=IndentIncrease, Escape=ClearHints, PgUp=GoPageUp, PgDn=GoPageDown, End=GoEnd, Home=GoHome, Left=GoLeft, Up=GoUp, Right=GoRight, Down=GoDown, Ins=ReplaceMode, Del=DeleteCharRight, F3=FindNext, Shift+Tab=IndentDecrease, Shift+PgUp=GoPageUpWithSelection, Shift+PgDn=GoPageDownWithSelection, Shift+End=GoEndWithSelection, Shift+Home=GoHomeWithSelection, Shift+Left=GoLeftWithSelection, Shift+Up=GoUpWithSelection, Shift+Right=GoRightWithSelection, Shift+Down=GoDownWithSelection, Shift+Ins=Paste, Shift+Del=Cut, Ctrl+Back=ClearWordLeft, Ctrl+Space=AutocompleteMenu, Ctrl+End=GoLastLine, Ctrl+Home=GoFirstLine, Ctrl+Left=GoWordLeft, Ctrl+Up=ScrollUp, Ctrl+Right=GoWordRight, Ctrl+Down=ScrollDown, Ctrl+Ins=Copy, Ctrl+Del=ClearWordRight, Ctrl+0=ZoomNormal, Ctrl+A=SelectAll, Ctrl+B=BookmarkLine, Ctrl+C=Copy, Ctrl+E=MacroExecute, Ctrl+F=FindDialog, Ctrl+G=GoToDialog, Ctrl+H=ReplaceDialog, Ctrl+I=AutoIndentChars, Ctrl+M=MacroRecord, Ctrl+N=GoNextBookmark, Ctrl+R=Redo, Ctrl+U=UpperCase, Ctrl+V=Paste, Ctrl+X=Cut, Ctrl+Z=Undo, Ctrl+Add=ZoomIn, Ctrl+Subtract=ZoomOut, Ctrl+OemMinus=NavigateBackward, Ctrl+Shift+End=GoLastLineWithSelection, Ctrl+Shift+Home=GoFirstLineWithSelection, Ctrl+Shift+Left=GoWordLeftWithSelection, Ctrl+Shift+Right=GoWordRightWithSelection, Ctrl+Shift+B=UnbookmarkLine, Ctrl+Shift+C=CommentSelected, Ctrl+Shift+N=GoPrevBookmark, Ctrl+Shift+U=LowerCase, Ctrl+Shift+OemMinus=NavigateForward, Alt+Back=Undo, Alt+Up=MoveSelectedLinesUp, Alt+Down=MoveSelectedLinesDown, Alt+F=FindChar, Alt+Shift+Left=GoLeft_ColumnSelectionMode, Alt+Shift+Up=GoUp_ColumnSelectionMode, Alt+Shift+Right=GoRight_ColumnSelectionMode, Alt+Shift+Down=GoDown_ColumnSelectionMode, Ctrl+D=CloneLine")]
public string Hotkeys {
get { return HotkeysMapping.ToString(); }
set { HotkeysMapping = HotkeysMapping.Parse(value); }
Expand Down Expand Up @@ -1005,8 +1005,7 @@ public Language Language
set
{
language = value;
if (SyntaxHighlighter != null)
SyntaxHighlighter.InitStyleSchema(language);
SyntaxHighlighter = SyntaxHighlighter.CreateSyntaxHighlighter(this, language);
Invalidate();
}
}
Expand All @@ -1020,13 +1019,13 @@ public Language Language

/// <summary>
/// XML file with description of syntax highlighting.
/// This property works only with Language == Language.Custom.
/// This property works only with Language == Language.FromXMLfile.
/// </summary>
[Browsable(true)]
[DefaultValue(null)]
[Editor(typeof (FileNameEditor), typeof (UITypeEditor))]
[Description(
"XML file with description of syntax highlighting. This property works only with Language == Language.Custom."
"XML file with description of syntax highlighting. This property works only with Language == Language.FromXMLfile."
)]
public string DescriptionFile
{
Expand Down Expand Up @@ -2901,10 +2900,7 @@ internal int GetOrSetStyleLayerIndex(Style style)

public static SizeF GetCharSize(Font font, char c)
{
Size sz2 = TextRenderer.MeasureText("<" + c.ToString() + ">", font);
Size sz3 = TextRenderer.MeasureText("<>", font);

return new SizeF(sz2.Width - sz3.Width + 1, /*sz2.Height*/font.Height);
return CharSizeCache.GetCharSize(font, c);
}

[DllImport("Imm32.dll")]
Expand Down Expand Up @@ -3752,6 +3748,10 @@ private void DoAction(FCTBAction action)
BookmarkLine(Selection.Start.iLine);
break;

case FCTBAction.CloneLine:
CloneLine(Selection);
break;

case FCTBAction.GoNextBookmark:
GotoNextBookmark(Selection.Start.iLine);
break;
Expand Down Expand Up @@ -4118,6 +4118,21 @@ public virtual void BookmarkLine(int iLine)
bookmarks.Add(iLine);
}

/// <summary>
/// Clones current line
/// </summary>
public virtual void CloneLine(Range selection)
{
// expand selection
selection.Expand();
// get text of selected lines
string text = Environment.NewLine + selection.Text;
// move caret to end of selected lines
selection.Start = selection.End;
// insert text
InsertText(text);
}

/// <summary>
/// Unbookmarks current line
/// </summary>
Expand Down Expand Up @@ -4731,7 +4746,7 @@ public virtual int CalcAutoIndent(int iLine)

EventHandler<AutoIndentEventArgs> calculator = AutoIndentNeeded;
if (calculator == null)
if (Language != Language.Custom && SyntaxHighlighter != null)
if (Language != Language.Custom && Language != Language.FromXMLfile && SyntaxHighlighter != null)
calculator = SyntaxHighlighter.AutoIndentNeeded;
else
calculator = CalcAutoIndentShiftByCodeFolding;
Expand Down Expand Up @@ -7265,10 +7280,7 @@ public virtual void OnSyntaxHighlight(TextChangedEventArgs args)

if (SyntaxHighlighter != null)
{
if (Language == Language.Custom && !string.IsNullOrEmpty(DescriptionFile))
SyntaxHighlighter.HighlightSyntax(DescriptionFile, range);
else
SyntaxHighlighter.HighlightSyntax(Language, range);
SyntaxHighlighter.SyntaxHighlight(range);
}

#if debug
Expand Down
Loading