public static SyntaxLanguage GetDynamicLanguage(string name, float windowBrightness) { SyntaxLanguage language2; string key = name + windowBrightness; if (_dynamicLanguages.ContainsKey(key)) { return(_dynamicLanguages[key]); } using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("LINQPad.Lexers." + name + ".xml")) { language2 = DynamicSyntaxLanguage.LoadFromXml(stream, 0); } foreach (HighlightingStyle style in language2.get_HighlightingStyles()) { if ((style.get_ForeColor().GetBrightness() < 0.2f) && (windowBrightness < 0.4f)) { style.set_ForeColor(Color.White); } else if ((windowBrightness < 0.4f) && (style.get_ForeColor() == Color.Blue)) { style.set_ForeColor(Color.FromArgb(130, 170, 0xff)); } else if ((windowBrightness < 0.4f) && ((style.get_ForeColor() == Color.Red) || (style.get_ForeColor() == Color.FromArgb(220, 20, 20)))) { style.set_ForeColor(Color.FromArgb(210, 90, 90)); } else if ((windowBrightness < 0.4f) && (style.get_ForeColor() == Color.Green)) { style.set_ForeColor(Color.FromArgb(100, 200, 110)); } } _dynamicLanguages[key] = language2; return(language2); }
/// <summary> /// Loads two languages and creates a state transition from XML to C# within ASP-style directives. /// </summary> private void CreateDirectiveXmlToCSharpLanguage() { string outputSyntaxFilePath = Slyce.Common.SyntaxEditorHelper.GetLanguageFileName(TemplateContentLanguage.PlainText); string scriptSyntaxFilePath = Slyce.Common.SyntaxEditorHelper.GetLanguageFileName(TemplateContentLanguage.CSharp); if (!File.Exists(outputSyntaxFilePath)) { MessageBox.Show(this, "Language syntax file could not be loaded: " + outputSyntaxFilePath); return; } if (!File.Exists(scriptSyntaxFilePath)) { MessageBox.Show(this, "Language syntax file could not be loaded: " + scriptSyntaxFilePath); return; } DynamicSyntaxLanguage language = DynamicSyntaxLanguage.LoadFromXml(outputSyntaxFilePath, 0); DynamicSyntaxLanguage cSharpLanguage = DynamicSyntaxLanguage.LoadFromXml(scriptSyntaxFilePath, 0); cSharpLanguage.DefaultLexicalState.DefaultHighlightingStyle.ForeColor = Color.Red; language.Tag = "TemplateLanguage"; cSharpLanguage.Tag = "ScriptLanguage"; // Mark that updating is starting language.IsUpdating = true; cSharpLanguage.IsUpdating = true; // Add a highlighting style language.HighlightingStyles.Add(new HighlightingStyle("ASPDirectiveDelimiterStyle", null, Color.Black, Color.Yellow)); // Create a new lexical state DynamicLexicalState lexicalState = new DynamicLexicalState(0, "ASPDirectiveState"); lexicalState.DefaultTokenKey = "ASPDirectiveDefaultToken"; lexicalState.DefaultHighlightingStyle = language.HighlightingStyles["DefaultStyle"]; lexicalState.LexicalStateTransitionLexicalState = cSharpLanguage.LexicalStates["DefaultState"]; language.LexicalStates.Add(lexicalState); // Add the new lexical state at the beginning of the child states... // Remember that with an NFA regular expression, the first match is taken... // So since a < scope pattern is already in place, we must insert the new one before it language.LexicalStates["DefaultState"].ChildLexicalStates.Insert(0, lexicalState); // Create a lexical scope with a lexical state transition DynamicLexicalScope lexicalScope = new DynamicLexicalScope(); lexicalState.LexicalScopes.Add(lexicalScope); lexicalScope.StartLexicalPatternGroup = new LexicalPatternGroup(LexicalPatternType.Explicit, "ASPDirectiveStartToken", language.HighlightingStyles["ASPDirectiveDelimiterStyle"], "#"); lexicalScope.EndLexicalPatternGroup = new LexicalPatternGroup(LexicalPatternType.Explicit, "ASPDirectiveEndToken", language.HighlightingStyles["ASPDirectiveDelimiterStyle"], "#"); // Mark that updating is complete (since linking is complete, the flag setting // will filter from the XML language into the C# language) language.IsUpdating = false; syntaxEditorFilename.Document.Language = language; }
public SyntaxLanguage GetSyntaxLanguage(string file) { string documentType = this.GetDocumentType(file); if (documentType == "text.C#") { if (CodeProjectService.csharpSyntaxLanguage == null) { CodeProjectService.csharpSyntaxLanguage = new CSharpSyntaxLanguage(); } return((SyntaxLanguage)CodeProjectService.csharpSyntaxLanguage); } if (documentType == "text.VB") { if (CodeProjectService.vbSyntaxLanguage == null) { CodeProjectService.vbSyntaxLanguage = new VBSyntaxLanguage(); } return((SyntaxLanguage)CodeProjectService.vbSyntaxLanguage); } if (documentType == "text.JS") { if (CodeProjectService.jsSyntaxLanguage == null) { using (Stream stream = (Stream) new MemoryStream(Microsoft.Expression.Code.FileTable.GetByteArray("Resources\\Actipro.JS.xml"))) CodeProjectService.jsSyntaxLanguage = DynamicSyntaxLanguage.LoadFromXml(stream, 0); } return((SyntaxLanguage)CodeProjectService.jsSyntaxLanguage); } if (documentType == "text.C++") { if (CodeProjectService.cppSyntaxLanguage == null) { using (Stream stream = (Stream) new MemoryStream(Microsoft.Expression.Code.FileTable.GetByteArray("Resources\\Actipro.CPP.xml"))) { try { CodeProjectService.cppSyntaxLanguage = DynamicSyntaxLanguage.LoadFromXml(stream, 0); } catch (Exception ex) { throw; } } } return((SyntaxLanguage)CodeProjectService.cppSyntaxLanguage); } if (documentType == "text") { return(SyntaxLanguage.PlainText); } return((SyntaxLanguage)null); }
/// <summary> /// Gets a DynamicSyntaxLanguage instance given a Languages enum. /// </summary> /// <param name="language"></param> /// <returns></returns> public static DynamicSyntaxLanguage GetDynamicLanguage(TemplateContentLanguage language) { string outputSyntaxFilePath = GetLanguageFileName(language); //if (!File.Exists(outputSyntaxFilePath)) //{ // throw new Exception("Language syntax file could not be loaded: " + outputSyntaxFilePath); //} string filename = Path.GetFileName(outputSyntaxFilePath); //Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format("Slyce.Common.SyntaxFiles.{0}", filename)); return(DynamicSyntaxLanguage.LoadFromXml(outputSyntaxFilePath, 0)); //return DynamicSyntaxLanguage.LoadFromXml(stream, 0); }
/// <summary> /// Loads two languages and creates a state transition from XML to C# within ASP-style directives. /// </summary> public static void SetupEditorTemplateAndScriptLanguages(SyntaxEditor editor, TemplateContentLanguage textLanguage, ScriptLanguageTypes scriptLanguage, string delimiterStart, string delimiterEnd) { DynamicSyntaxLanguage language = GetDynamicLanguage(textLanguage); DynamicSyntaxLanguage cSharpLanguage = GetDynamicLanguage(GetScriptingLanguage(scriptLanguage)); language.AutomaticOutliningBehavior = AutomaticOutliningBehavior.PostSemanticParse; cSharpLanguage.AutomaticOutliningBehavior = AutomaticOutliningBehavior.PostSemanticParse; language.Tag = "TemplateLanguage"; cSharpLanguage.Tag = "ScriptLanguage"; // Mark that updating is starting language.IsUpdating = true; cSharpLanguage.IsUpdating = true; // Add StateTransitions to current language as well nested languages eg: HTML -> JavaScript, VBScript etc. for (int i = 0; i <= language.ChildLanguages.Count; i++) { DynamicSyntaxLanguage lan; if (i == language.ChildLanguages.Count) { lan = language; } else { lan = (DynamicSyntaxLanguage)language.ChildLanguages[i]; } // Add a highlighting style lan.HighlightingStyles.Add(new HighlightingStyle("ASPDirectiveDelimiterStyle", null, Color.Black, ASP_DIRECTIVE_COLOUR)); lan.AutomaticOutliningBehavior = AutomaticOutliningBehavior.SemanticParseDataChange; // Create a new lexical state DynamicLexicalState lexicalState = new DynamicLexicalState(0, "ASPDirectiveState"); lexicalState.DefaultTokenKey = "ASPDirectiveDefaultToken"; lexicalState.DefaultHighlightingStyle = lan.HighlightingStyles["DefaultStyle"]; lexicalState.LexicalStateTransitionLexicalState = cSharpLanguage.LexicalStates["DefaultState"]; lan.LexicalStates.Add(lexicalState); // Add the new lexical state at the beginning of the child states... // Remember that with an NFA regular expression, the first match is taken... // So since a < scope pattern is already in place, we must insert the new one before it lan.LexicalStates["DefaultState"].ChildLexicalStates.Insert(0, lexicalState); #region Extra Transition points - Eg: comments if (lan.LexicalStates.IndexOf("XMLCommentState") >= 0) // C# { lan.LexicalStates["XMLCommentState"].ChildLexicalStates.Insert(0, lexicalState); // Added this to ensure that transitions can occur in XML Comments } if (lan.LexicalStates.IndexOf("CommentState") >= 0) // C# { lan.LexicalStates["CommentState"].ChildLexicalStates.Insert(0, lexicalState); // Added this to ensure that transitions can occur in XML Comments } if (lan.LexicalStates.IndexOf("StringState") >= 0) // SQL { // Note: Had to modify the RegexPatternGroup for StringState in ActiproSoftware.SQL.xml to: <RegexPatternGroup TokenKey="StringDefaultToken" PatternValue="[^<^']+" /> lan.LexicalStates["StringState"].ChildLexicalStates.Insert(0, lexicalState); // Added this to ensure that transitions can occur in XML Comments } if (lan.LexicalStates.IndexOf("SquareStringState") >= 0) // SQL { lan.LexicalStates["SquareStringState"].ChildLexicalStates.Insert(0, lexicalState); // Added this to ensure that transitions can occur in XML Comments } if (lan.LexicalStates.IndexOf("MultiLineCommentState") >= 0) // SQL { lan.LexicalStates["MultiLineCommentState"].ChildLexicalStates.Insert(0, lexicalState); // Added this to ensure that transitions can occur in XML Comments } if (lan.LexicalStates.IndexOf("StartTagState") >= 0) // HTML { lan.LexicalStates["StartTagState"].ChildLexicalStates.Insert(0, lexicalState); // Added this to ensure that transitions can occur in XML Comments } if (lan.LexicalStates.IndexOf("StartTagAttributeStringValueState") >= 0) // HTML { lan.LexicalStates["StartTagAttributeStringValueState"].ChildLexicalStates.Insert(0, lexicalState); // Added this to ensure that transitions can occur in XML Comments } if (lan.LexicalStates.IndexOf("StartTagAttributeState") >= 0) // HTML { lan.LexicalStates["StartTagAttributeState"].ChildLexicalStates.Insert(0, lexicalState); // Added this to ensure that transitions can occur in XML Comments } if (lan.LexicalStates.IndexOf("StartTagAttributeValueState") >= 0) // HTML { lan.LexicalStates["StartTagAttributeValueState"].ChildLexicalStates.Insert(0, lexicalState); // Added this to ensure that transitions can occur in XML Comments } // Create a lexical scope with a lexical state transition DynamicLexicalScope lexicalScope = new DynamicLexicalScope(); lexicalState.LexicalScopes.Add(lexicalScope); lexicalScope.StartLexicalPatternGroup = new LexicalPatternGroup(LexicalPatternType.Explicit, "ASPDirectiveStartToken", lan.HighlightingStyles["ASPDirectiveDelimiterStyle"], delimiterStart); lexicalScope.StartLexicalPatternGroup.LookBehindPattern = @"\\{2}|([^\\]|^)"; // @"\\{2}|[^\\]"; lexicalScope.EndLexicalPatternGroup = new LexicalPatternGroup(LexicalPatternType.Explicit, "ASPDirectiveEndToken", lan.HighlightingStyles["ASPDirectiveDelimiterStyle"], delimiterEnd); lexicalScope.AncestorEndScopeCheckEnabled = false; } #endregion // Mark that updating is complete (since linking is complete, the flag setting // will filter from the XML language into the C# language) language.IsUpdating = false; editor.Document.Language = language; }