📄 syntax.cs
字号:
// Syntax.cs
// Copyright (c) 2001 Mike Krueger
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Drawing;
using System.Diagnostics;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
namespace SharpDevelop.Internal.Text {
public class FontContainer
{
static Font defaultfont = null;
static Font boldfont = null;
static Font italicfont = null;
static Font bolditalicfont = null;
public static Font BoldFont {
get {
Debug.Assert(boldfont != null, "SharpDevelop.Internal.Text.FontContainer : boldfont == null");
return boldfont;
}
}
public static Font ItalicFont {
get {
Debug.Assert(italicfont != null, "SharpDevelop.Internal.Text.FontContainer : italicfont == null");
return italicfont;
}
}
public static Font BoldItalicFont {
get {
Debug.Assert(bolditalicfont != null, "SharpDevelop.Internal.Text.FontContainer : bolditalicfont == null");
return bolditalicfont;
}
}
public static Font DefaultFont {
get {
if(defaultfont == null)
DefaultFont = new Font("Courier New", 10);
return defaultfont;
}
set {
defaultfont = value;
boldfont = new Font(defaultfont, FontStyle.Bold);
italicfont = new Font(defaultfont, FontStyle.Italic);
bolditalicfont = new Font(defaultfont, FontStyle.Bold | FontStyle.Italic);
}
}
public static void LoadFonts()
{
// defaultfont = SharpDevelop.Tool.Data.Option.GetProperty("SharpDevelop.Internal.Text.FontContainer.DefaultFont", new Font("Courier New", 10));
}
}
public class SyntaxColor
{
Color color;
Color backgroundcolor = System.Drawing.Color.WhiteSmoke;
bool bold = false;
bool italic = false;
string name = "";
public static Color ParseColor(string c)
{
int a = 255;
int offset = 0;
if (c.Length > 7) {
offset = 2;
a = Int32.Parse(c.Substring(1,2), NumberStyles.HexNumber);
}
int r = Int32.Parse(c.Substring(1 + offset,2), NumberStyles.HexNumber);
int g = Int32.Parse(c.Substring(3 + offset,2), NumberStyles.HexNumber);
int b = Int32.Parse(c.Substring(5 + offset,2), NumberStyles.HexNumber);
return Color.FromArgb(a, r, g, b);
}
public SyntaxColor(XmlElement el)
{
Debug.Assert(el != null, "SharpDevelop.Internal.Text.SyntaxColor(XmlElement el) : el == null");
if (el.Attributes["Name"] != null)
name = el.Attributes["Name"].InnerText;
if (el.Attributes["Bold"] != null)
bold = Boolean.Parse(el.Attributes["Bold"].InnerText);
if (el.Attributes["Italic"] != null)
italic = Boolean.Parse(el.Attributes["Italic"].InnerText);
if (el.Attributes["Color"] != null) {
string c = el.Attributes["Color"].InnerText;
if (c[0] == '#') {
color = ParseColor(c);
} else
color = (Color)(Color.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, Color, new object[0]);
} else {
color = Color.Transparent; // to set it to the default value.
}
if (el.Attributes["BackgroundColor"] != null) {
string c = el.Attributes["BackgroundColor"].InnerText;
if (c[0] == '#') {
backgroundcolor = ParseColor(c);
} else
backgroundcolor = (Color)(Color.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, Color, new object[0]);
}
}
public SyntaxColor(Color color, bool bold, bool italic)
{
this.color = color;
this.bold = bold;
this.italic = italic;
}
public string Name {
get {
return name;
}
}
public bool Bold {
get {
return bold;
}
set {
bold = value;
}
}
public bool Italic {
get {
return italic;
}
set {
italic = value;
}
}
public Color BackgroundColor {
get {
return backgroundcolor;
}
set {
backgroundcolor = value;
}
}
public Color Color {
get {
return color;
}
set {
color = value;
}
}
public Font Font {
get {
if (Bold) {
return Italic ? FontContainer.BoldItalicFont : FontContainer.BoldFont;
}
return Italic ? FontContainer.ItalicFont :FontContainer.DefaultFont;
}
}
}
public class Span
{
public bool StopEOL;
public SyntaxColor Color;
public string Begin = null;
public string End = null;
public string Name = null;
public string Rule = null;
public Span(XmlElement span)
{
Color = new SyntaxColor(span);
if (span.Attributes["Rule"] != null)
Rule = span.Attributes["Rule"].InnerText;
Name = span.Attributes["Name"].InnerText;
StopEOL = Boolean.Parse(span.Attributes["StopAtEol"].InnerText);
Begin = span["Begin"].InnerText;
if (span["End"] != null)
End = span["End"].InnerText;
}
}
public class PrevMarker
{
public string What;
public SyntaxColor Color;
public PrevMarker(XmlElement mark)
{
Color = new SyntaxColor(mark);
What = mark.InnerText;
}
}
public class RuleSet
{
public LookupTable KeyWords;
public ArrayList Spans = new ArrayList();
public LookupTable PrevMarkers;
public bool IgnoreCase = false;
string name = null;
public string Delimeters = "";
public string Reference = null;
public string Name {
get {
return name;
}
set {
name = value;
}
}
public RuleSet(XmlElement el)
{
XmlNodeList nodes = el.GetElementsByTagName("KeyWords");
if (el.Attributes["Name"] != null)
Name = el.Attributes["Name"].InnerText;
if (el.Attributes["Reference"] != null)
Reference = el.Attributes["Reference"].InnerText;
if (el.Attributes["IgnoreCase"] != null)
IgnoreCase = Boolean.Parse(el.Attributes["IgnoreCase"].InnerText);
if (el["Delimeters"] != null)
Delimeters = el["Delimeters"].InnerText;
else
Delimeters = "";
// Spans = new LookupTable(!IgnoreCase);
KeyWords = new LookupTable(!IgnoreCase);
PrevMarkers = new LookupTable(!IgnoreCase);
foreach (XmlElement el2 in nodes) {
SyntaxColor color = new SyntaxColor(el2);
XmlNodeList keys = el2.GetElementsByTagName("Key");
foreach (XmlElement node in keys)
KeyWords[node.Attributes["Word"].InnerText] = color;
}
nodes = el.GetElementsByTagName("Span");
foreach (XmlElement el2 in nodes) {
Spans.Add(new Span(el2));
/*
Span span = new Span(el2);
Spans[span.Begin] = span;*/
}
nodes = el.GetElementsByTagName("MarkPrevious");
foreach (XmlElement el2 in nodes) {
PrevMarker prev = new PrevMarker(el2);
PrevMarkers[prev.What] = prev;
}
}
}
public class Syntax
{
public ArrayList Rules = new ArrayList();
public Hashtable Properties = new Hashtable();
public SyntaxColor defaultColor;
public SyntaxColor CaretColor = null;
public SyntaxColor SelectionColor = null;
public SyntaxColor HRulerColor = null;
public SyntaxColor SpaceMarkerColor = null;
public SyntaxColor TabMarkerColor = null;
public SyntaxColor InvalidLineColor = null;
public SyntaxColor LineNumberColor = null;
public SyntaxColor EolMarkerColor = null;
public SyntaxColor digitColor = null;
public SyntaxColor BookmarkColor = null;
public SyntaxColor CaretmarkerColor = null;
public SyntaxColor FoldLine = null;
public SyntaxColor FoldMarker = null;
public Color BackgroundColor;
public string Name;
public bool DoIndent;
public string[] Extensions;
public bool DefaultSyntax = false;
public static Syntax[] SyntaxDefinitions;
public static void LoadSyntaxDefinitions()
{
string[] modes = Directory.GetFiles(Application.StartupPath + "\\modes");
SyntaxDefinitions = new Syntax[modes.Length];
for (int i = 0; i < modes.Length; ++i) {
SyntaxDefinitions[i] = new Syntax(modes[i]);
}
}
public Syntax()
{
DefaultSyntax = true;
defaultColor = new SyntaxColor(Color.Black, false, false);
LineNumberColor = new SyntaxColor(Color.Gray, false, false);
EolMarkerColor = new SyntaxColor(Color.DarkCyan, false, false);
digitColor = new SyntaxColor(Color.Blue, false, false);
InvalidLineColor= new SyntaxColor(Color.Red, false, false);
BackgroundColor = Color.White;
}
public Syntax(String filename)
{
XmlDocument doc = new XmlDocument();
doc.Load(filename);
Name = doc.DocumentElement.Attributes["Name"].InnerText;
if (doc.DocumentElement.Attributes["Extensions"]!= null)
Extensions = doc.DocumentElement.Attributes["Extensions"].InnerText.Split(new char[] { '|' });
if (doc.DocumentElement.Attributes["Indent"]!= null)
DoIndent = Boolean.Parse(doc.DocumentElement.Attributes["Indent"].InnerText);
ReadEnvironmentSettings(doc.DocumentElement["Environment"]);
// ReadProperties(doc.DocumentElement["PROPERTIES"]);
digitColor = new SyntaxColor(doc.DocumentElement["Digits"]);
XmlNodeList nodes = doc.DocumentElement.GetElementsByTagName("RuleSet");
foreach (XmlElement el2 in nodes)
Rules.Add(new RuleSet(el2));
}
public void ReadEnvironmentSettings(XmlElement el)
{
Color tmpcolor = Color.Black;
string c = el.Attributes["DefaultColor"].InnerText;
if (c[0] == '#') {
tmpcolor = SyntaxColor.ParseColor(c);
} else
tmpcolor = (Color)(tmpcolor.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, tmpcolor, new object[0]);
defaultColor = new SyntaxColor(tmpcolor, false, false);
c = el.Attributes["BackgroundColor"].InnerText;
if (c[0] == '#') {
BackgroundColor = SyntaxColor.ParseColor(c);
} else
BackgroundColor = (Color)(tmpcolor.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, tmpcolor, new object[0]);
HRulerColor = new SyntaxColor(el["HRuler"]);
SelectionColor = new SyntaxColor(el["Selection"]);
CaretColor = new SyntaxColor(el["Cursor"]);
LineNumberColor = new SyntaxColor(el["Linenumbers"]);
InvalidLineColor = new SyntaxColor(el["InvalidLines"]);
EolMarkerColor = new SyntaxColor(el["EOLMarkers"]);
SpaceMarkerColor = new SyntaxColor(el["SpaceMarkers"]);
TabMarkerColor = new SyntaxColor(el["TabMarkers"]);
CaretmarkerColor = new SyntaxColor(el["CaretMarker"]);
BookmarkColor = new SyntaxColor(el["BookMarks"]);
FoldLine = new SyntaxColor(el["FoldLine"]);
FoldMarker = new SyntaxColor(el["FoldMarker"]);
}
public void ReadProperties(XmlElement el)
{
XmlNodeList nodes = el.GetElementsByTagName("Properties");
foreach (XmlElement el2 in nodes)
Properties[el2.Attributes["Name"].InnerText] = el2.Attributes["Value"].InnerText;
}
public SyntaxColor DigitColor {
get {
return digitColor;
}
}
public SyntaxColor GetColor(string keyWord, int offset, int length)
{
if (Rules != null)
foreach (RuleSet rule in Rules)
if (rule.Name == null) {
if (rule.Reference != null) { // reference to other syntax definition
foreach (Syntax syn in SyntaxDefinitions) {
if (syn.Name == rule.Reference) {
return syn.GetColor(keyWord, offset, length);
}
}
}
SyntaxColor syncol = (SyntaxColor)rule.KeyWords[keyWord, offset, length];
if (syncol != null)
return syncol;
}
return null;
}
public RuleSet GetRuleSet(string name)
{
if (Rules != null)
foreach (RuleSet rule in Rules) {
if (rule.Reference != null) { // reference to other syntax definition
foreach (Syntax syn in SyntaxDefinitions) {
if (syn.Name == rule.Reference) {
return syn.GetRuleSet(null);
}
}
}
if (rule.Name == name) {
return rule;
}
}
return null;
}
public SyntaxColor GetColor(string name, string keyWord, int offset, int length)
{
if (Rules != null)
foreach (RuleSet rule in Rules)
if (rule.Name == name) {
if (rule.Reference != null) {
foreach (Syntax syn in SyntaxDefinitions)
if (syn.Name == rule.Reference) {
return syn.GetColor(keyWord, offset, length);
}
}
SyntaxColor syncol = (SyntaxColor)rule.KeyWords[keyWord, offset, length];
if (syncol != null)
return syncol;
}
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -