public MainWindow()
        {
            InitializeComponent();
            CompilerUtils.DeleteFilesByWildcards("Test*.exe");

            ViewModel.Window = this;


            TextEditor.Text = ViewModel.SourceCode;
            TextEditor.TextArea.TextEntering       += textEditor_TextArea_TextEntering;
            TextEditor.TextArea.TextEntered        += textEditor_TextArea_TextEntered;
            TextEditor.TextArea.IndentationStrategy = new ICSharpCode.AvalonEdit.Indentation.CSharp.CSharpIndentationStrategy(TextEditor.Options);
            foldingStrategy = new BraceFoldingStrategy();

            DispatcherTimer foldingUpdateTimer = new DispatcherTimer();

            foldingUpdateTimer.Interval = TimeSpan.FromSeconds(2);
            foldingUpdateTimer.Tick    += foldingUpdateTimer_Tick;
            foldingUpdateTimer.Start();


            if (foldingManager == null)
            {
                foldingManager = FoldingManager.Install(TextEditor.TextArea);
            }
            foldingStrategy.UpdateFoldings(foldingManager, TextEditor.Document);

            // Dynamic syntax highlighting for Intermediate Representation
            var rules = IL.SyntaxHighlighting.MainRuleSet.Rules;

            var highlightingRule = new HighlightingRule();

            highlightingRule.Color = new HighlightingColor()
            {
                Foreground = new CustomizedBrush(Color.Green),
                FontWeight = FontWeight.FromOpenTypeWeight(130)
            };

            var wordList = ILOperatorKeywords;
            var regex    = String.Format(@"\b({0})\b", String.Join("|", wordList));

            highlightingRule.Regex = new Regex(regex);

            rules.Add(highlightingRule);

            highlightingRule       = new HighlightingRule();
            highlightingRule.Color = new HighlightingColor()
            {
                Foreground = new CustomizedBrush(Color.Red)
            };

            wordList = ILKeywords;
            regex    = String.Format(@"\b({0})\b", String.Join("|", wordList));
            highlightingRule.Regex = new Regex(regex);

            rules.Add(highlightingRule);


            TextEditor.Text = VisualCompilerConstants.InitialCode;
        }
Example #2
0
        private void InitializeSyntaxHighlighter()
        {
            var stream = GetType().Assembly.GetManifestResourceStream(
                "Dynamo.UI.Resources." + Configurations.HighlightingFile);

            this.InnerTextEditor.SyntaxHighlighting = HighlightingLoader.Load(
                new XmlTextReader(stream), HighlightingManager.Instance);

            // Highlighting Digits
            var rules = this.InnerTextEditor.SyntaxHighlighting.MainRuleSet.Rules;

            var   highlightingRule = new HighlightingRule();
            Color color            = (Color)ColorConverter.ConvertFromString("#2585E5");

            highlightingRule.Color = new HighlightingColor()
            {
                Foreground = new CustomizedBrush(color)
            };

            // These Regex's must match with the grammars in the DS ATG for digits
            // Refer to the 'number' and 'float' tokens in Start.atg
            //*******************************************************************************
            // number = digit {digit} .
            // float = digit {digit} '.' digit {digit} [('E' | 'e') ['+'|'-'] digit {digit}].
            //*******************************************************************************

            // number with optional floating point
            string number = @"(-?\d+(\.[0-9]+)?)";
            // optional exponent
            string exponent = @"([eE][+-]?[0-9]+)?";

            highlightingRule.Regex = new Regex(number + exponent);

            rules.Add(highlightingRule);
        }
Example #3
0
        /// <summary>
        /// Create hight lighting rule for number.
        /// </summary>
        /// <returns></returns>
        public static HighlightingRule CreateNumberHighlightingRule()
        {
            var digitRule = new HighlightingRule();

            Color color = (Color)ColorConverter.ConvertFromString("#2585E5");

            digitRule.Color = new HighlightingColor()
            {
                Foreground = new CustomizedBrush(color)
            };

            // These Regex's must match with the grammars in the DS ATG for digits
            // Refer to the 'number' and 'float' tokens in Start.atg
            //*******************************************************************************
            // number = digit {digit} .
            // float = digit {digit} '.' digit {digit} [('E' | 'e') ['+'|'-'] digit {digit}].
            //*******************************************************************************

            string digit                     = @"(-?\b\d+)";
            string floatingPoint             = @"(\.[0-9]+)";
            string numberWithOptionalDecimal = digit + floatingPoint + "?";

            string exponent           = @"([eE][+-]?[0-9]+)";
            string numberWithExponent = digit + floatingPoint + exponent;

            digitRule.Regex = new Regex(numberWithExponent + "|" + numberWithOptionalDecimal);

            return(digitRule);
        }
Example #4
0
        private HighlightingRuleSet Wrap(HighlightingRuleSet ruleSet)
        {
            if (ruleSet is null)
            {
                return(null);
            }

            if (!String.IsNullOrEmpty(ruleSet.Name) &&
                NamedRuleSet.TryGetValue(ruleSet.Name, out var cachedRule))
            {
                return(cachedRule);
            }

            if (Converted.TryGetValue(ruleSet, out var cachedRule2))
            {
                return(cachedRule2);
            }

            var copySet = new HighlightingRuleSet();

            copySet.Name = ruleSet.Name;

            Converted[ruleSet] = copySet;
            if (!String.IsNullOrEmpty(copySet.Name))
            {
                NamedRuleSet[copySet.Name] = copySet;
            }

            foreach (var baseSpan in ruleSet.Spans)
            {
                if (baseSpan is null)
                {
                    continue;
                }

                var copySpan = new HighlightingSpan();
                copySpan.StartExpression        = baseSpan.StartExpression;
                copySpan.EndExpression          = baseSpan.EndExpression;
                copySpan.RuleSet                = Wrap(baseSpan.RuleSet);
                copySpan.StartColor             = Wrap(baseSpan.StartColor);
                copySpan.SpanColor              = Wrap(baseSpan.SpanColor);
                copySpan.EndColor               = Wrap(baseSpan.EndColor);
                copySpan.SpanColorIncludesStart = baseSpan.SpanColorIncludesStart;
                copySpan.SpanColorIncludesEnd   = baseSpan.SpanColorIncludesEnd;

                copySet.Spans.Add(copySpan);
            }

            foreach (var baseRule in ruleSet.Rules)
            {
                var copyRule = new HighlightingRule();
                copyRule.Regex = baseRule.Regex;
                copyRule.Color = Wrap(baseRule.Color);

                copySet.Rules.Add(copyRule);
            }

            return(copySet);
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);

                if (attributes != null && attributes.Length > 0)
                {
                    this.VersionText = "v" + ((AssemblyFileVersionAttribute)attributes[0]).Version;
                }

                Assembly resourceAssembly = Assembly.GetExecutingAssembly();
                string[] manifests        = resourceAssembly.GetManifestResourceNames();

                using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Expressive.Playground.ExpressiveSyntax.xshd"))
                {
                    using (XmlTextReader reader = new XmlTextReader(s))
                    {
                        textEditor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);

                        // Dynamic syntax highlighting for your own purpose
                        var rules = textEditor.SyntaxHighlighting.MainRuleSet.Rules;

                        var highlightingRule = new HighlightingRule();
                        highlightingRule.Color = new HighlightingColor {
                            Foreground = new CustomBrush((Color) new ColorConverter().ConvertFromInvariantString("#569cd6"))
                        };

                        // TODO this needs to be populated from the Expression class or some other exposed class
                        var wordList = _functions.Select(f => f.Text).ToArray();
                        //String[] wordList = new[]
                        //{
                        //    // Date
                        //    "Days",
                        //    // Logical
                        //    "If", "In",
                        //    // Mathematical
                        //    "Abs", "Acos", "Asin", "Atan", "Ceiling", "Cos", "Count", "Exp", "Floor", "IEEERemainder", "Log10", "Log", "Max", "Min", "Pow", "Sign", "Sin", "Sqrt", "Sum", "Tan", "Truncate",
                        //    // Statistical
                        //    "Average", "Mean", "Median", "Mode",
                        //    // String
                        //    "Length", "PadLeft", "PadRight", "Regex", "Substring",
                        //};
                        String regex = String.Format(@"\b({0})\w*\b", String.Join("|", wordList));
                        highlightingRule.Regex = new Regex(regex, RegexOptions.IgnoreCase);

                        rules.Add(highlightingRule);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            this.InitializeTextMarkerService();
            this.textEditor.Focus();
        }
        public HighlightingDefinition AddRule(string expression, HighlightingColor color)
        {
            var rule = new HighlightingRule
            {
                Regex = expression.ToRegex(),
                Color = color
            };

            return(AddRule(rule));
        }
            public object VisitRuleSet(XshdRuleSet ruleSet)
            {
                HighlightingRuleSet rs = ruleSetDict[ruleSet];

                if (processedRuleSets.Contains(ruleSet))
                {
                    return(rs);
                }
                if (!processingStartedRuleSets.Add(ruleSet))
                {
                    throw Error(ruleSet, "RuleSet cannot be processed because it contains cyclic <Import>");
                }

                bool oldIgnoreCase = ignoreCase;

                if (ruleSet.IgnoreCase != null)
                {
                    ignoreCase = ruleSet.IgnoreCase.Value;
                }

                rs.Name = ruleSet.Name;

                foreach (XshdElement element in ruleSet.Elements)
                {
                    object o = element.AcceptVisitor(this);
                    HighlightingRuleSet elementRuleSet = o as HighlightingRuleSet;
                    if (elementRuleSet != null)
                    {
                        Merge(rs, elementRuleSet);
                    }
                    else
                    {
                        HighlightingSpan span = o as HighlightingSpan;
                        if (span != null)
                        {
                            rs.Spans.Add(span);
                        }
                        else
                        {
                            HighlightingRule elementRule = o as HighlightingRule;
                            if (elementRule != null)
                            {
                                rs.Rules.Add(elementRule);
                            }
                        }
                    }
                }

                ignoreCase = oldIgnoreCase;
                processedRuleSets.Add(ruleSet);

                return(rs);
            }
            public object VisitRuleSet(XshdRuleSet ruleSet)
            {
                HighlightingRuleSet rs;

                if (ruleSet.Name != null)
                {
                    rs = def.ruleSetDict[ruleSet.Name];
                }
                else
                {
                    rs = new HighlightingRuleSet();
                }

                bool oldIgnoreCase = ignoreCase;

                if (ruleSet.IgnoreCase != null)
                {
                    ignoreCase = ruleSet.IgnoreCase.Value;
                }

                rs.Name = ruleSet.Name;

                foreach (XshdElement element in ruleSet.Elements)
                {
                    object o = element.AcceptVisitor(this);
                    HighlightingRuleSet elementRuleSet = o as HighlightingRuleSet;
                    if (elementRuleSet != null)
                    {
                        Merge(rs, elementRuleSet);
                    }
                    else
                    {
                        HighlightingSpan span = o as HighlightingSpan;
                        if (span != null)
                        {
                            rs.Spans.Add(span);
                        }
                        else
                        {
                            HighlightingRule elementRule = o as HighlightingRule;
                            if (elementRule != null)
                            {
                                rs.Rules.Add(elementRule);
                            }
                        }
                    }
                }

                ignoreCase = oldIgnoreCase;

                return(rs);
            }
Example #9
0
        public static HighlightingRule SimpleRule()
        {
            var newHighlightRule = new HighlightingRule();

            newHighlightRule.Color = new HighlightingColor()
            {
                FontWeight = FontWeights.Bold,
                Foreground = new SimpleHighlightingBrush(Color.FromArgb(244, 0, 51, 0))
            };

            newHighlightRule.Regex = new Regex(@"\b(?>procedure|call)\b");
            return(newHighlightRule);
        }
        public void ProcessFile(string path)
        {
            //this will read in the labels
            this._originalContents = File.ReadAllText(path);
            this.fileContents      = this._originalContents;
            this.UpdateLabels();

            //create the highlighting rule -- will be held by reference in general rules
            _HighlightingRule = new HighlightingRule();


            Window1.highlightingDefinition.MainRuleSet.Rules.Add(_HighlightingRule);
            UpdateHighlighter();
        }
Example #11
0
        private HighlightingRule CreateClassHighlightRule()
        {
            Color color = (Color)ColorConverter.ConvertFromString("#2E998F");
            var classHighlightRule = new HighlightingRule();
            classHighlightRule.Color = new HighlightingColor()
            {
                Foreground = new Dynamo.Wpf.Views.CodeBlockEditorUtils.CustomizedBrush(color)
            };

            var engineController = dynamoViewModel.Model.EngineController;
            var wordList = engineController.CodeCompletionServices.GetClasses();
            String regex = String.Format(@"\b({0})({0})?\b", String.Join("|", wordList));
            classHighlightRule.Regex = new Regex(regex);

            return classHighlightRule;
        }
Example #12
0
        /// <summary>
        /// Create hight lighting rule for method.
        /// </summary>
        /// <returns></returns>
        public static HighlightingRule CreateMethodHighlightRule(EngineController engineController)
        {
            Color color = (Color)ColorConverter.ConvertFromString("#417693");
            var   methodHighlightRule = new HighlightingRule
            {
                Color = new HighlightingColor()
                {
                    Foreground = new CodeHighlightingRuleFactory.CustomizedBrush(color)
                }
            };

            var    wordList = engineController.CodeCompletionServices.GetGlobals();
            String regex    = String.Format(@"\b({0})({0})?\b", String.Join("|", wordList));

            methodHighlightRule.Regex = new Regex(regex);

            return(methodHighlightRule);
        }
Example #13
0
        /// <summary>
        /// Create hight lighting rule for class.
        /// </summary>
        /// <param name="engineController"></param>
        /// <returns></returns>
        public static HighlightingRule CreateClassHighlightRule(EngineController engineController)
        {
            Color color = (Color)ColorConverter.ConvertFromString("#2E998F");
            var   classHighlightRule = new HighlightingRule
            {
                Color = new HighlightingColor()
                {
                    Foreground = new CodeHighlightingRuleFactory.CustomizedBrush(color)
                }
            };

            var    wordList = engineController.CodeCompletionServices.GetClasses();
            String regex    = String.Format(@"\b({0})\b", String.Join("|", wordList));

            classHighlightRule.Regex = new Regex(regex);

            return(classHighlightRule);
        }
Example #14
0
        private HighlightingRule CreateMethodHighlightRule()
        {
            Color color = (Color)ColorConverter.ConvertFromString("#417693");
            var   methodHighlightRule = new HighlightingRule();

            methodHighlightRule.Color = new HighlightingColor()
            {
                Foreground = new CustomizedBrush(color)
            };

            var engineController = this.dynamoViewModel.Model.EngineController;

            var    wordList = engineController.CodeCompletionServices.GetGlobals();
            String regex    = String.Format(@"\b({0})({0})?\b", String.Join("|", wordList));

            methodHighlightRule.Regex = new Regex(regex);

            return(methodHighlightRule);
        }
        public static void AddHighlighting(this TextEditor editor, IEnumerable <string> strings, Color color)
        {
            var stringsList = strings.ToList();

            if (!stringsList.Any())
            {
                return;
            }
            var highlightingRule = new HighlightingRule();

            highlightingRule.Color = new HighlightingColor
            {
                Foreground = new SimpleHighlightingBrush(color),
                FontWeight = FontWeights.Bold
            };

            var regex = string.Format(@"\b({0})\b", string.Join("|", strings));

            highlightingRule.Regex = new Regex(regex);
            editor.SyntaxHighlighting.MainRuleSet.Rules.Add(highlightingRule);
        }
 public HighlightingDefinition AddRule(HighlightingRule rule)
 {
     MainRuleSet.Rules.Add(rule);
     AddColor(rule.Color);
     return(this);
 }