Skip to content

Commit

Permalink
Merge pull request #4 from mcneel/sbaer/languages
Browse files Browse the repository at this point in the history
Sbaer/languages
  • Loading branch information
sbaer authored Mar 23, 2018
2 parents fbfda2a + d7994fb commit e6bd8ed
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 89 deletions.
48 changes: 28 additions & 20 deletions src/Eto.CodeEditor.Wpf/CodeEditorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public CodeEditorHandler()
SetupTheme();
}

public void SetProgrammingLanguage(ProgrammingLanguage language, string[] keywordSets)
{
Language = language;
if(keywordSets!=null)
{
for( int i=0; i<keywordSets.Length; i++ )
{
SetKeywords(i, keywordSets[i]);
}
}
}

public string Text
{
get => WinFormsControl.Text;
Expand All @@ -31,38 +43,31 @@ public void SetKeywords(int set, string keywords)
WinFormsControl.SetKeywords(set, keywords);
}

public Lexer Lexer
ProgrammingLanguage _language = ProgrammingLanguage.None;
public ProgrammingLanguage Language
{
get { return ToCodeEditor(WinFormsControl.Lexer); }
set { WinFormsControl.Lexer = ToScintillaNet(value); }
get { return _language; }
set
{
_language = value;
WinFormsControl.Lexer = ToScintillaNet(value);
}
}

static ScintillaNET.Lexer ToScintillaNet(Lexer l)
static ScintillaNET.Lexer ToScintillaNet(ProgrammingLanguage l)
{
switch (l)
{
case Lexer.Cpp:
case ProgrammingLanguage.CSharp:
case ProgrammingLanguage.GLSL:
return ScintillaNET.Lexer.Cpp;
case Lexer.VB:
case ProgrammingLanguage.VB:
return ScintillaNET.Lexer.Vb;
case Lexer.Python:
case ProgrammingLanguage.Python:
return ScintillaNET.Lexer.Python;
}
return ScintillaNET.Lexer.Null;
}
static Lexer ToCodeEditor(ScintillaNET.Lexer l)
{
switch (l)
{
case ScintillaNET.Lexer.Cpp:
return Lexer.Cpp;
case ScintillaNET.Lexer.Vb:
return Lexer.VB;
case ScintillaNET.Lexer.Python:
return Lexer.Python;
}
return Lexer.Cpp;
}

public string FontName
{
Expand Down Expand Up @@ -162,6 +167,9 @@ void SetupTheme()
//WinFormsControl.Styles[ScintillaNET.Style.LineNumber].BackColor = System.Drawing.Color.White;
//WinFormsControl.Styles[ScintillaNET.Style.LineNumber].ForeColor = System.Drawing.Color.CadetBlue;

FontName = "Consolas";
FontSize = 10;
LineNumberColumnWidth = 40;
}
}
}
74 changes: 36 additions & 38 deletions src/Eto.CodeEditor.XamMac/CodeEditorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class CodeEditorHandler : Eto.Mac.Forms.MacView<ScintillaView, CodeEditor
public CodeEditorHandler()
{
Control = new ScintillaView();

FontName = "Menlo";
FontSize = 14;
LineNumberColumnWidth = 40;
}

public string Text
Expand All @@ -41,42 +45,35 @@ public string Text
public void SetKeywords(int set, string keywords)
{
Control.SetKeywords(set, keywords);
}

public void SetProgrammingLanguage(ProgrammingLanguage language, string[] keywordSets)
{
int which = ScintillaNET.NativeMethods.SCLEX_CPP;
switch (language)
{
case ProgrammingLanguage.CSharp:
case ProgrammingLanguage.GLSL:
which = ScintillaNET.NativeMethods.SCLEX_CPP;
break;
case ProgrammingLanguage.VB:
which = ScintillaNET.NativeMethods.SCLEX_VB;
break;
case ProgrammingLanguage.Python:
which = ScintillaNET.NativeMethods.SCLEX_PYTHON;
break;
}
Control.SetGeneralProperty(ScintillaNET.NativeMethods.SCI_SETLEXER, which, 0);

if (keywordSets != null)
{
for (int i = 0; i < keywordSets.Length; i++)
{
SetKeywords(i, keywordSets[i]);
}
}
}

public Lexer Lexer
{
get
{
nint prop = Control.GetGeneralProperty(ScintillaNET.NativeMethods.SCI_GETLEXER);
switch (prop)
{
case ScintillaNET.NativeMethods.SCLEX_CPP:
return Lexer.Cpp;
case ScintillaNET.NativeMethods.SCLEX_VB:
return Lexer.VB;
case ScintillaNET.NativeMethods.SCLEX_PYTHON:
return Lexer.Python;
}
return Lexer.Cpp;
}
set
{
int which = ScintillaNET.NativeMethods.SCLEX_CPP;
switch (value)
{
case Lexer.Cpp:
which = ScintillaNET.NativeMethods.SCLEX_CPP;
break;
case Lexer.VB:
which = ScintillaNET.NativeMethods.SCLEX_VB;
break;
case Lexer.Python:
which = ScintillaNET.NativeMethods.SCLEX_PYTHON;
break;
}
Control.SetGeneralProperty(ScintillaNET.NativeMethods.SCI_SETLEXER, which, 0);
}
}

public string FontName
{
Expand Down Expand Up @@ -116,9 +113,10 @@ public int LineNumberColumnWidth

public void SetColor(Section section, Eto.Drawing.Color foreground, Eto.Drawing.Color background)
{
NSColor fg = NSColor.FromRgb(foreground.R, foreground.G, foreground.B);
NSColor bg = NSColor.FromRgb(background.R, background.G, background.B);

string fg = foreground.ToHex(false);
string bg = background.ToHex(false);
//NSColor fg = NSColor.FromRgba(foreground.R, foreground.G, foreground.B, foreground.A);
//NSColor bg = NSColor.FromRgba(background.R, background.G, background.B, background.A);
if (section == Section.Comment)
{
if (foreground != Eto.Drawing.Colors.Transparent)
Expand All @@ -143,7 +141,7 @@ public void SetColor(Section section, Eto.Drawing.Color foreground, Eto.Drawing.
if (background != Eto.Drawing.Colors.Transparent)
{
Control.SetColorProperty(NativeMethods.SCI_STYLESETBACK, NativeMethods.SCE_C_WORD, bg);
Control.SetColorProperty(NativeMethods.SCI_STYLESETBACK, NativeMethods.SCE_C_WORD2, fg);
Control.SetColorProperty(NativeMethods.SCI_STYLESETBACK, NativeMethods.SCE_C_WORD2, bg);
}
}

Expand Down
45 changes: 33 additions & 12 deletions src/Eto.CodeEditor/CodeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@ namespace Eto.CodeEditor
[Handler(typeof(IHandler))]
public class CodeEditor : Control
{
static string[] GetKeywords(ProgrammingLanguage language)
{
switch (language)
{
case ProgrammingLanguage.CSharp:
return new string[]
{
"abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe using virtual while",
"bool byte char class const decimal double enum float int long sbyte short static string struct uint ulong ushort void"
};
default:
return new string[0];
}
}

readonly ProgrammingLanguage _language;
public CodeEditor(ProgrammingLanguage language)
{
_language = language;
Handler.SetProgrammingLanguage( language, GetKeywords(language) );

SetColor(Section.Comment, Drawing.Colors.Gray, Drawing.Colors.White);
SetColor(Section.Keyword, Drawing.Colors.SeaGreen, Drawing.Colors.White);
SetColor(Section.LineNumber, Drawing.Colors.Gray, Drawing.Colors.White);
}

new IHandler Handler => (IHandler)base.Handler;

public string Text
Expand All @@ -15,15 +41,9 @@ public string Text
set => Handler.Text = value;
}

public void SetKeywords(int set, string keywords)
{
Handler.SetKeywords(set, keywords);
}

public Lexer Lexer
public ProgrammingLanguage Language
{
get => Handler.Lexer;
set => Handler.Lexer = value;
get => _language;
}

public string FontName
Expand Down Expand Up @@ -52,8 +72,7 @@ public void SetColor(Section section, Eto.Drawing.Color foreground, Eto.Drawing.
public new interface IHandler : Control.IHandler
{
string Text { get; set; }
void SetKeywords(int set, string keywords);
Lexer Lexer { get; set; }
void SetProgrammingLanguage(ProgrammingLanguage language, string[] keywordSets);
string FontName { get; set; }
int FontSize { get; set; }
int LineNumberColumnWidth { get; set; }
Expand All @@ -68,9 +87,11 @@ public enum Section
LineNumber
}

public enum Lexer
public enum ProgrammingLanguage
{
Cpp,
None,
CSharp,
GLSL,
VB,
Python
}
Expand Down
20 changes: 1 addition & 19 deletions test/Eto.CodeEditor.TestApp/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public MainForm()
ClientSize = new Size(400, 400);


var editor = new CodeEditor
var editor = new CodeEditor(ProgrammingLanguage.CSharp)
{
Text =
@"// Just some sample code
Expand All @@ -22,24 +22,6 @@ public MainForm()
}"
};

editor.Lexer = Lexer.Cpp;
editor.SetKeywords(0, "abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe using virtual while");
editor.SetKeywords(1, "bool byte char class const decimal double enum float int long sbyte short static string struct uint ulong ushort void");
editor.SetColor(Section.Comment, Colors.Gray, Colors.Transparent);
editor.SetColor(Section.Keyword, Colors.CadetBlue, Colors.Transparent);
editor.SetColor(Section.LineNumber, Colors.CadetBlue, Colors.White);
if (Eto.Forms.Application.Instance.Platform.IsMac)
{
editor.FontName = "Menlo";
editor.FontSize = 14;
editor.LineNumberColumnWidth = 40;
}
else
{
editor.FontName = "Consolas";
editor.FontSize = 10;
editor.LineNumberColumnWidth = 60;
}
Content = editor;
}
}
Expand Down

0 comments on commit e6bd8ed

Please sign in to comment.