Example #1
0
        internal static void UpdateCodeViewer(bool show, CompilationResult compilationResult, Action <object> selectionDelegate, SourceCodePhases sourceIndex = SourceCodePhases.Initial, Type pluginIndex = null)
        {
            if (compilationResult == null)
            {
                return;
            }

            // Build the collection of CodeViewerCodeLine used by the CodeViewer.
            // They contain the line of code, the matching semantic node's instance ID (stored in a metadata object),
            // and a collection of messages by alert types (ex: error, warning and info)
            // The matching semantic node' instance ID is stored to allow for easy panning to the node when selecting a code line.

            IList <string> splitSourceCode;

            if (pluginIndex != null)
            {
                splitSourceCode = compilationResult.pluginSourceCode[pluginIndex].Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            }
            else
            {
                splitSourceCode = compilationResult.sourceCode[(int)sourceIndex].Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            }

            Dictionary <int, List <CompilerError> > errorPerLine = new Dictionary <int, List <CompilerError> >();

            foreach (var compilerError in compilationResult.errors)
            {
                // Line Index + Description + SemanticNode Instance Id
                var match = s_ErrorRowColumnRegex.Match(compilerError.description);
                if (!match.Success)
                {
                    continue;
                }

                // The line index reported is 1-indexed.
                int codeLineIndex = Convert.ToInt32(match.Groups[1].Value) - 1;

                if (!errorPerLine.ContainsKey(codeLineIndex))
                {
                    errorPerLine[codeLineIndex] = new List <CompilerError>();
                }
                errorPerLine[codeLineIndex].Add(compilerError);
            }


            var document = new Document(selectionDelegate);

            for (var lineIndex = 0; lineIndex < splitSourceCode.Count; lineIndex++)
            {
                string sourceCodeLine = splitSourceCode[lineIndex];

                var line = new Line(lineIndex + 1, sourceCodeLine);

                if (errorPerLine.ContainsKey(lineIndex))
                {
                    var decorator = LineDecorator.CreateError(string.Join(Environment.NewLine, errorPerLine[lineIndex].Select(error => error.description)));

                    line.AddDecorator(decorator);

                    var sourceNode = errorPerLine[lineIndex].FirstOrDefault()?.sourceNode;
                    if (sourceNode)
                    {
                        line.Metadata = sourceNode.GetInstanceID();
                    }
                }

                document.AddLine(line);
            }

            if (show)
            {
                EditorWindow.GetWindow(typeof(CodeViewerWindow));
            }

            CodeViewerWindow.SetDocument(document);
        }
Example #2
0
 void ShowCode()
 {
     CodeViewerWindow.SetDocument(CodeViewerWindow.SplitCode(m_CurrentModel.Generate()));
 }