示例#1
0
        /// <summary>
        /// Get current Gherkin language setting by scanning first 5 lines
        /// </summary>
        /// <param name="document"></param>
        /// <returns>language defined in tag or English as default language</returns>
        public static string CurrentLanguage(ICSharpCode.AvalonEdit.Document.TextDocument document)
        {
            var location = new Ast.Location();

            ICSharpCode.AvalonEdit.Document.DocumentLine line = document.GetLineByNumber(1);
            while (line != null)
            {
                string line_text = document.GetText(line.Offset, line.TotalLength);
                Token  token     = new Token(new GherkinLine(line_text, line.LineNumber), location);
                try
                {
                    if (s_TokenMatcher.Match_Language(token) && IsSupported(token.MatchedText))
                    {
                        return(token.MatchedText);
                    }
                    if (line.LineNumber > 5)
                    {
                        return(DEFAULT_LANGUAGE);
                    }
                    line = line.NextLine;
                }
                catch
                {
                    return(DEFAULT_LANGUAGE);
                }
            }
            return(DEFAULT_LANGUAGE);
        }
        public void EmptyCommentNode()
        {
            string     xml   = "<!----><root/>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//comment()");

            ServiceContainer container = new ServiceContainer();

            container.AddService(typeof(MockTextMarkerService), new MockTextMarkerService());

            IDocument doc = new ICSharpCode.AvalonEdit.Document.TextDocument()
            {
                ServiceProvider = container
            };

            doc.Text = xml;
            XPathNodeTextMarker xpathNodeMarker = new XPathNodeTextMarker(doc);

            xpathNodeMarker.AddMarkers(nodes);

            ITextMarkerService service = doc.GetService(typeof(MockTextMarkerService)) as ITextMarkerService;
            List <ITextMarker> markers = new List <ITextMarker>(service.TextMarkers);

            Assert.AreEqual(0, markers.Count);
            Assert.AreEqual(1, nodes.Length);
        }
        public void NamespaceQuery()
        {
            string xml =
                "<?xml version='1.0'?>\r\n" +
                "<Xml1></Xml1>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//namespace::*");

            ServiceContainer container = new ServiceContainer();

            container.AddService(typeof(MockTextMarkerService), new MockTextMarkerService());

            IDocument doc = new ICSharpCode.AvalonEdit.Document.TextDocument()
            {
                ServiceProvider = container
            };

            doc.Text = xml;
            XPathNodeTextMarker xpathNodeMarker = new XPathNodeTextMarker(doc);

            xpathNodeMarker.AddMarkers(nodes);

            ITextMarkerService service = doc.GetService(typeof(MockTextMarkerService)) as ITextMarkerService;
            List <ITextMarker> markers = new List <ITextMarker>(service.TextMarkers);

            Assert.AreEqual(0, markers.Count);
            Assert.AreEqual(1, nodes.Length);
        }
示例#4
0
        /// <summary>
        /// Class constructor
        /// </summary>
        /// <param name="document"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public BracketSearchResult SearchBracket(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset)
        {
            if (offset > 0)
            {
                char c           = document.GetCharAt(offset - 1);
                int  index       = openingBrackets.IndexOf(c);
                int  otherOffset = -1;
                if (index > -1)
                {
                    otherOffset = SearchBracketForward(document, offset, openingBrackets[index], closingBrackets[index]);
                }

                index = closingBrackets.IndexOf(c);
                if (index > -1)
                {
                    otherOffset = SearchBracketBackward(document, offset - 2, openingBrackets[index], closingBrackets[index]);
                }

                if (otherOffset > -1)
                {
                    return(new BracketSearchResult(Math.Min(offset - 1, otherOffset), 1,
                                                   Math.Max(offset - 1, otherOffset), 1));
                }
            }

            return(null);
        }
示例#5
0
 private void UpdateFolding(ICSharpCode.AvalonEdit.Document.TextDocument doc)
 {
     if (foldingStrategy != null)
     {
         foldingStrategy.UpdateFoldings(foldingManager, doc);
     }
 }
示例#6
0
 public SimpleCompletionProvider(ScriptCs.ScriptServices service, ICSharpCode.AvalonEdit.Document.TextDocument text)
 {
     this._service = service;
     this.text     = text;
     eProvider     = new EnumProvider();
     sProvider     = new StaticProvider();
 }
 void SyntaxHighlighterViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
 {
     if (e.PropertyName == "Text")
     {
         Document = new ICSharpCode.AvalonEdit.Document.TextDocument(Text);
     }
 }
示例#8
0
        public static IEnumerable <string> AllIdents(this ICSharpCode.AvalonEdit.Document.TextDocument doc, int minLength = 2)
        {
            var  builder    = new StringBuilder();
            bool isString   = false;
            char stringchar = '\0';

            for (int i = 0; i < doc.TextLength; i++)
            {
                var c = doc.GetCharAt(i);
                if (isString)
                {
                    if (c == stringchar)
                    {
                        isString = false;
                    }
                }
                else if ((builder.Length == 0 ? char.IsLetter(c) : char.IsLetterOrDigit(c)) || c == '_')
                {
                    builder.Append(c);
                }
                else if (c == '"' || c == '\'')
                {
                    stringchar = c;
                    isString   = true;
                }
                else
                {
                    if (builder.Length >= minLength)
                    {
                        yield return(builder.ToString());
                    }
                    builder.Clear();
                }
            }
        }
示例#9
0
        public static string GetWordAround(this ICSharpCode.AvalonEdit.Document.TextDocument doc, int offset)
        {
            if (offset < 0 || offset >= doc.TextLength)
            {
                return(string.Empty);
            }
            int start, end;

            //find start
            for (start = offset; start >= 0; start--)
            {
                var c = doc.GetCharAt(start);
                if (!char.IsLetterOrDigit(c) && c != '_')
                {
                    start++;
                    break;
                }
            }
            //find end
            for (end = start; end < doc.TextLength; end++)
            {
                var c = doc.GetCharAt(end);
                if (!char.IsLetterOrDigit(c) && c != '_')
                {
                    break;
                }
            }
            return(doc.GetText(start, end - start));
        }
示例#10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TextModel" /> class.
 /// </summary>
 /// <param name="commandManager">The injected command manager.</param>
 /// <param name="menuService">The menu service.</param>
 public TextModel(ICommandManager commandManager, IMenuService menuService)
 {
     Document = new ICSharpCode.AvalonEdit.Document.TextDocument();
     _commandManager = commandManager;
     _menuService = menuService;
     Document.PropertyChanged += DocumentPropertyChanged;
     Document.TextChanged += DocumentOnTextChanged;
     OldText = "";
 }
示例#11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TextModel" /> class.
 /// </summary>
 /// <param name="commandManager">The injected command manager.</param>
 /// <param name="menuService">The menu service.</param>
 public TextModel(ICommandManager commandManager, IMenuService menuService)
 {
     Document                  = new ICSharpCode.AvalonEdit.Document.TextDocument();
     _commandManager           = commandManager;
     _menuService              = menuService;
     Document.PropertyChanged += DocumentPropertyChanged;
     Document.TextChanged     += DocumentOnTextChanged;
     OldText = "";
 }
示例#12
0
 private static int ScanLineStart(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset)
 {
     for (int i = offset - 1; i > 0; --i)
     {
         if (document.GetCharAt(i) == '\n')
         {
             return(i + 1);
         }
     }
     return(0);
 }
 public override void IndentLine(ICSharpCode.AvalonEdit.Document.TextDocument document, ICSharpCode.AvalonEdit.Document.DocumentLine line)
 {
     if (CodeEditorOptions.Instance.UseSmartIndentation)
     {
         base.IndentLine(document, line);
     }
     else
     {
         new DefaultIndentationStrategy().IndentLine(document, line);
     }
 }
示例#14
0
        private int QuickSearchBracketForward(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset, char openBracket, char closingBracket)
        {
            int brackets = 1;

            // try "quick find" - find the matching bracket if there is no string/comment in the way
            for (int i = offset; i < document.TextLength; ++i)
            {
                char ch = document.GetCharAt(i);
                if (ch == openBracket)
                {
                    ++brackets;
                }
                else if (ch == closingBracket)
                {
                    --brackets;
                    if (brackets == 0)
                    {
                        return(i);
                    }
                }
                else if (ch == '"')
                {
                    break;
                }
                else if (ch == '\'')
                {
                    break;
                }
                else if (ch == '/' && i > 0)
                {
                    if (document.GetCharAt(i - 1) == '/')
                    {
                        break;
                    }
                }
                else if (ch == '*' && i > 0)
                {
                    if (document.GetCharAt(i - 1) == '/')
                    {
                        break;
                    }
                }
            }
            return(-1);
        }
        public CompletionProvider(ScriptCs.ScriptServices service, ICSharpCode.AvalonEdit.Document.TextDocument text)
        {
            _simp = new SimpleCompletionProvider(service, text);

            var currentPath = Assembly.GetExecutingAssembly().Location;

            currentPath = Path.GetDirectoryName(currentPath);
            var f1 = Path.Combine(currentPath, "Microsoft.CodeAnalysis.Features.dll");
            var f2 = Path.Combine(currentPath, "Microsoft.CodeAnalysis.CSharp.Features");
            var f3 = Path.Combine(currentPath, "Microsoft.CodeAnalysis.Workspaces.Desktop");

            if (!File.Exists(f1))
            {
                File.WriteAllBytes(f1, Resources.Microsoft_CodeAnalysis_Features);
            }
            if (!File.Exists(f2))
            {
                File.WriteAllBytes(f2, Resources.Microsoft_CodeAnalysis_CSharp_Features);
            }
            if (!File.Exists(f3))
            {
                File.WriteAllBytes(f3, Resources.Microsoft_CodeAnalysis_Workspaces_Desktop);
            }

            _host = MefHostServices.Create(MefHostServices.DefaultAssemblies.Concat(new[]
            {
                Assembly.LoadFrom(f1),
                Assembly.LoadFrom(f2)
            }));

            _ws = new InteractiveWorkspace(_host);


            _parseOptions = new CSharpParseOptions(kind: SourceCodeKind.Interactive);

            _references = _assemblyTypes.Select(t =>
                                                MetadataReference.CreateFromAssembly(t.Assembly)).ToArray();
            _compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
                                                               usings: _assemblyTypes.Select(x => x.Namespace).ToImmutableArray());

            var container = new CompositionContainer(new AssemblyCatalog(typeof(ISignatureHelpProvider).Assembly),
                                                     CompositionOptions.DisableSilentRejection | CompositionOptions.IsThreadSafe);

            _signatureHelpProviders = container.GetExportedValues <ISignatureHelpProvider>().ToArray();
        }
		public void EmptyCommentNode()
		{
			string xml = "<!----><root/>";
			XPathQuery query = new XPathQuery(xml);
			XPathNodeMatch[] nodes = query.FindNodes("//comment()");
			
			ServiceContainer container = new ServiceContainer();
			container.AddService(typeof(MockTextMarkerService), new MockTextMarkerService());
			
			IDocument doc = new ICSharpCode.AvalonEdit.Document.TextDocument() { ServiceProvider = container };
			doc.Text = xml;
			XPathNodeTextMarker xpathNodeMarker = new XPathNodeTextMarker(doc);
			xpathNodeMarker.AddMarkers(nodes);
			
			ITextMarkerService service = doc.GetService(typeof(MockTextMarkerService)) as ITextMarkerService;
			List<ITextMarker> markers = new List<ITextMarker>(service.TextMarkers);
			
			Assert.AreEqual(0, markers.Count);
			Assert.AreEqual(1, nodes.Length);
		}
		public void NamespaceQuery()
		{
			string xml = 
				"<?xml version='1.0'?>\r\n" +
				"<Xml1></Xml1>";
			XPathQuery query = new XPathQuery(xml);
			XPathNodeMatch[] nodes = query.FindNodes("//namespace::*");
			
			ServiceContainer container = new ServiceContainer();
			container.AddService(typeof(MockTextMarkerService), new MockTextMarkerService());
			
			IDocument doc = new ICSharpCode.AvalonEdit.Document.TextDocument() { ServiceProvider = container };
			doc.Text = xml;
			XPathNodeTextMarker xpathNodeMarker = new XPathNodeTextMarker(doc);
			xpathNodeMarker.AddMarkers(nodes);
			
			ITextMarkerService service = doc.GetService(typeof(MockTextMarkerService)) as ITextMarkerService;
			List<ITextMarker> markers = new List<ITextMarker>(service.TextMarkers);
			
			Assert.AreEqual(0, markers.Count);
			Assert.AreEqual(1, nodes.Length);
		}
示例#18
0
        //--------------------------------------------------------------
        #region Creation & Cleanup
        //--------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="OutputViewModel" /> class.
        /// </summary>
        /// <param name="editor">The editor. Can be <see langword="null"/> at design-time.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="editor"/> is <see langword="null"/>.
        /// </exception>
        public OutputViewModel(IEditorService editor)
        {
            if (editor == null && !WindowsHelper.IsInDesignMode)
            {
                throw new ArgumentNullException(nameof(editor));
            }

            _editor      = editor;
            DisplayName  = "Output";
            IsPersistent = true;
            DockId       = DockIdString;
            //Icon = MultiColorGlyphs.Output;

            _buffers = new Dictionary <string, StringBuilder>
            {
                { DefaultView, new StringBuilder() }
            };
            Views = new ObservableCollection <string> {
                DefaultView
            };
            PrintPreviewCommand = new DelegateCommand(ShowPrintPreview, CanPrint);
            PrintCommand        = new DelegateCommand(Print, CanPrint);

            _nlogTarget = editor?.Services.GetInstance <INLogTarget>();
            if (_nlogTarget != null)
            {
                _buffers.Add(NLogView, new StringBuilder());
                Views.Add(NLogView);
            }
            _windowService = editor?.Services.GetInstance <IWindowService>().ThrowIfMissing();

            Output = new TextDocument {
                UndoStack = { SizeLimit = 0 }
            };
            SelectedView = DefaultView;
        }
示例#19
0
        private void SetupCodeEditor()
        {
            // Already created?
            if (workspace != null)
            {
                // Anything changed?
                if (workspace == Workspace /* && projectId == ProjectId*/)
                {
                    return;
                }

                CleanupCodeEditor();
            }

            // Check we have everything we need
            if (Workspace == null || codeEditor == null || ProjectId == null)
            {
                return;
            }

            this.workspace = Workspace;

            // Start with initial text
            var textDocument = new TextDocument(Text);

            sourceTextContainer              = new AvalonEditTextContainer(textDocument);
            sourceTextContainer.TextChanged += SourceTextContainer_TextChanged;

            var documentId = workspace.AddDocument(ProjectId, $"script-{Guid.NewGuid()}.cs", SourceCodeKind.Script, TextLoader.From(sourceTextContainer, VersionStamp.Create()));

            DocumentId = documentId;
            workspace.OpenDocument(sourceTextContainer, documentId, a => Dispatcher.Invoke(() => codeEditor.ProcessDiagnostics(a)));

            // Bind SourceTextContainer to UI
            codeEditor.BindSourceTextContainer(workspace, sourceTextContainer, documentId);
        }
示例#20
0
 protected override IHighlighter CreateHighlighter(TextView textView, ICSharpCode.AvalonEdit.Document.TextDocument document)
 {
     return(new CodeHighlighter(document, workspace, getSemanticModel));
 }
 protected override IHighlighter CreateHighlighter(TextView textView, TextDocument document)
 {
     return new RoslynSemanticHighlighter(document, _documentId, _roslynHost);
 }
 public BracketSearchResult SearchBracket(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset)
 {
     return(null);
 }
示例#23
0
        /// <inheritdoc/>
        protected override IEnumerable<NewFolding> CreateNewFoldings(
            AvalonEditDocument document, out int firstErrorOffset)
        {
            firstErrorOffset = -1;

            List<NewFolding> foldings = new List<NewFolding>();

            Stack<NewFolding> activeFoldings = new Stack<NewFolding>();
            Stack<int> activeIndentations = new Stack<int>();

            NewFolding activeFolding = null;
            int activeIndentation = -1;
            int endOffsetOfLastNonEmptyLine = 0;
            for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++)
            {
                var line = document.GetLineByNumber(lineNumber);

                // Determine indentation of line.
                int offset = line.Offset;
                int indentation = 0;
                bool isLineEmpty = true;
                for (int i = 0; i < line.Length; i++)
                {
                    if (char.IsWhiteSpace(document.GetCharAt(offset + i)))
                    {
                        indentation++;
                    }
                    else
                    {
                        // Found the first non-white space character.
                        isLineEmpty = false;
                        break;
                    }
                }

                // Skip empty lines.
                if (isLineEmpty)
                    continue;

                // If the indentation is less than the previous, then we close the last active
                // folding.
                if (indentation < activeIndentation)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    activeFolding.EndOffset = endOffsetOfLastNonEmptyLine;

                    // Keep foldings which span more than one line.
                    if (document.GetLineByOffset(activeFolding.StartOffset) != document.GetLineByOffset(activeFolding.EndOffset))
                        foldings.Add(activeFolding);

                    if (activeFoldings.Count > 0)
                    {
                        activeFolding = activeFoldings.Pop();
                        activeIndentation = activeIndentations.Pop();
                    }
                    else
                    {
                        activeFolding = null;
                        activeIndentation = -1;
                    }

                    // Test same line again. The previous folding could also end on this line.
                    lineNumber--;
                    continue;
                }

                endOffsetOfLastNonEmptyLine = line.EndOffset;

                // If the indentation is larger than the previous indentation, we start a new
                // folding.
                if (indentation > 0 && indentation > activeIndentation)
                {
                    // Store current folding on stack.
                    if (activeFolding != null)
                    {
                        activeFoldings.Push(activeFolding);
                        activeIndentations.Push(activeIndentation);
                    }

                    activeFolding = new NewFolding { StartOffset = offset + indentation, };
                    activeIndentation = indentation;
                }
            }

            // Close all open foldings.
            while (activeFoldings.Count > 0)
            {
                var folding = activeFoldings.Pop();
                folding.EndOffset = endOffsetOfLastNonEmptyLine;
                foldings.Add(folding);
            }

            foldings.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset));
            return foldings;
        }
示例#24
0
        //--------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="OutputViewModel" /> class.
        /// </summary>
        /// <param name="editor">The editor. Can be <see langword="null"/> at design-time.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="editor"/> is <see langword="null"/>.
        /// </exception>
        public OutputViewModel(IEditorService editor)
        {
            if (editor == null && !WindowsHelper.IsInDesignMode)
                throw new ArgumentNullException(nameof(editor));

            _editor = editor;
            DisplayName = "Output";
            IsPersistent = true;
            DockId = DockIdString;
            //Icon = MultiColorGlyphs.Output;

            _buffers = new Dictionary<string, StringBuilder>
            {
                { DefaultView, new StringBuilder() }
            };
            Views = new ObservableCollection<string> { DefaultView };
            PrintPreviewCommand = new DelegateCommand(ShowPrintPreview, CanPrint);
            PrintCommand = new DelegateCommand(Print, CanPrint);

            _nlogTarget = editor?.Services.GetInstance<INLogTarget>();
            if (_nlogTarget != null)
            {
                _buffers.Add(NLogView, new StringBuilder());
                Views.Add(NLogView);
            }
            _windowService = editor?.Services.GetInstance<IWindowService>().ThrowIfMissing();

            Output = new TextDocument { UndoStack = { SizeLimit = 0 } };
            SelectedView = DefaultView;
        }
示例#25
0
        private int SearchBracketBackward(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset, char openBracket, char closingBracket)
        {
            if (offset + 1 >= document.TextLength)
            {
                return(-1);
            }
            // this method parses a c# document backwards to find the matching bracket

            // first try "quick find" - find the matching bracket if there is no string/comment in the way
            int quickResult = QuickSearchBracketBackward(document, offset, openBracket, closingBracket);

            if (quickResult >= 0)
            {
                return(quickResult);
            }

            // we need to parse the line from the beginning, so get the line start position
            int linestart = ScanLineStart(document, offset + 1);

            // we need to know where offset is - in a string/comment or in normal code?
            // ignore cases where offset is in a block comment
            int starttype = GetStartType(document, linestart, offset + 1);

            if (starttype == 1)
            {
                return(-1); // start position is in a comment
            }

            // I don't see any possibility to parse a C# document backwards...
            // We have to do it forwards and push all bracket positions on a stack.
            Stack <int> bracketStack = new Stack <int>();
            bool        blockComment = false;
            bool        lineComment  = false;
            bool        inChar       = false;
            bool        inString     = false;
            bool        verbatim     = false;

            for (int i = 0; i <= offset; ++i)
            {
                char ch = document.GetCharAt(i);
                switch (ch)
                {
                case '\r':
                case '\n':
                    lineComment = false;
                    inChar      = false;
                    if (!verbatim)
                    {
                        inString = false;
                    }
                    break;

                case '/':
                    if (blockComment)
                    {
                        Debug.Assert(i > 0);
                        if (document.GetCharAt(i - 1) == '*')
                        {
                            blockComment = false;
                        }
                    }
                    if (!inString && !inChar && i + 1 < document.TextLength)
                    {
                        if (!blockComment && document.GetCharAt(i + 1) == '/')
                        {
                            lineComment = true;
                        }
                        if (!lineComment && document.GetCharAt(i + 1) == '*')
                        {
                            blockComment = true;
                        }
                    }
                    break;

                case '"':
                    if (!(inChar || lineComment || blockComment))
                    {
                        if (inString && verbatim)
                        {
                            if (i + 1 < document.TextLength && document.GetCharAt(i + 1) == '"')
                            {
                                ++i;              // skip escaped quote
                                inString = false; // let the string go
                            }
                            else
                            {
                                verbatim = false;
                            }
                        }
                        else if (!inString && offset > 0 && document.GetCharAt(i - 1) == '@')
                        {
                            verbatim = true;
                        }
                        inString = !inString;
                    }
                    break;

                case '\'':
                    if (!(inString || lineComment || blockComment))
                    {
                        inChar = !inChar;
                    }
                    break;

                case '\\':
                    if ((inString && !verbatim) || inChar)
                    {
                        ++i; // skip next character
                    }
                    break;

                default:
                    if (ch == openBracket)
                    {
                        if (!(inString || inChar || lineComment || blockComment))
                        {
                            bracketStack.Push(i);
                        }
                    }
                    else if (ch == closingBracket)
                    {
                        if (!(inString || inChar || lineComment || blockComment))
                        {
                            if (bracketStack.Count > 0)
                            {
                                bracketStack.Pop();
                            }
                        }
                    }
                    break;
                }
            }
            if (bracketStack.Count > 0)
            {
                return((int)bracketStack.Pop());
            }
            return(-1);
        }
示例#26
0
        private int SearchBracketForward(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset, char openBracket, char closingBracket)
        {
            bool inString = false;
            bool inChar   = false;
            bool verbatim = false;

            bool lineComment  = false;
            bool blockComment = false;

            if (offset < 0)
            {
                return(-1);
            }

            // first try "quick find" - find the matching bracket if there is no string/comment in the way
            int quickResult = QuickSearchBracketForward(document, offset, openBracket, closingBracket);

            if (quickResult >= 0)
            {
                return(quickResult);
            }

            // we need to parse the line from the beginning, so get the line start position
            int linestart = ScanLineStart(document, offset);

            // we need to know where offset is - in a string/comment or in normal code?
            // ignore cases where offset is in a block comment
            int starttype = GetStartType(document, linestart, offset);

            if (starttype != 0)
            {
                return(-1);          // start position is in a comment/string
            }
            int brackets = 1;

            while (offset < document.TextLength)
            {
                char ch = document.GetCharAt(offset);
                switch (ch)
                {
                case '\r':
                case '\n':
                    lineComment = false;
                    inChar      = false;
                    if (!verbatim)
                    {
                        inString = false;
                    }
                    break;

                case '/':
                    if (blockComment)
                    {
                        Debug.Assert(offset > 0);
                        if (document.GetCharAt(offset - 1) == '*')
                        {
                            blockComment = false;
                        }
                    }
                    if (!inString && !inChar && offset + 1 < document.TextLength)
                    {
                        if (!blockComment && document.GetCharAt(offset + 1) == '/')
                        {
                            lineComment = true;
                        }
                        if (!lineComment && document.GetCharAt(offset + 1) == '*')
                        {
                            blockComment = true;
                        }
                    }
                    break;

                case '"':
                    if (!(inChar || lineComment || blockComment))
                    {
                        if (inString && verbatim)
                        {
                            if (offset + 1 < document.TextLength && document.GetCharAt(offset + 1) == '"')
                            {
                                ++offset;         // skip escaped quote
                                inString = false; // let the string go
                            }
                            else
                            {
                                verbatim = false;
                            }
                        }
                        else if (!inString && offset > 0 && document.GetCharAt(offset - 1) == '@')
                        {
                            verbatim = true;
                        }
                        inString = !inString;
                    }
                    break;

                case '\'':
                    if (!(inString || lineComment || blockComment))
                    {
                        inChar = !inChar;
                    }
                    break;

                case '\\':
                    if ((inString && !verbatim) || inChar)
                    {
                        ++offset; // skip next character
                    }
                    break;

                default:
                    if (ch == openBracket)
                    {
                        if (!(inString || inChar || lineComment || blockComment))
                        {
                            ++brackets;
                        }
                    }
                    else if (ch == closingBracket)
                    {
                        if (!(inString || inChar || lineComment || blockComment))
                        {
                            --brackets;
                            if (brackets == 0)
                            {
                                return(offset);
                            }
                        }
                    }
                    break;
                }
                ++offset;
            }
            return(-1);
        }
示例#27
0
        /// <summary>
        /// Gets the type of code at offset.<br/>
        /// 0 = Code,<br/>
        /// 1 = Comment,<br/>
        /// 2 = String<br/>
        /// Block comments and multiline strings are not supported.
        /// </summary>
        private static int GetStartType(ICSharpCode.AvalonEdit.Document.TextDocument document, int linestart, int offset)
        {
            bool inString = false;
            bool inChar   = false;
            bool verbatim = false;
            int  result   = 0;

            for (int i = linestart; i < offset; i++)
            {
                switch (document.GetCharAt(i))
                {
                case '/':
                    if (!inString && !inChar && i + 1 < document.TextLength)
                    {
                        if (document.GetCharAt(i + 1) == '/')
                        {
                            result = 1;
                        }
                    }
                    break;

                case '"':
                    if (!inChar)
                    {
                        if (inString && verbatim)
                        {
                            if (i + 1 < document.TextLength && document.GetCharAt(i + 1) == '"')
                            {
                                ++i;              // skip escaped quote
                                inString = false; // let the string go on
                            }
                            else
                            {
                                verbatim = false;
                            }
                        }
                        else if (!inString && i > 0 && document.GetCharAt(i - 1) == '@')
                        {
                            verbatim = true;
                        }
                        inString = !inString;
                    }
                    break;

                case '\'':
                    if (!inString)
                    {
                        inChar = !inChar;
                    }
                    break;

                case '\\':
                    if ((inString && !verbatim) || inChar)
                    {
                        ++i; // skip next character
                    }
                    break;
                }
            }

            return((inString || inChar) ? 2 : result);
        }
示例#28
0
 protected override IHighlighter CreateHighlighter(TextView textView, TextDocument document)
 {
     return(new RoslynSemanticHighlighter(document, _documentId, _roslynHost));
 }
示例#29
0
 protected override IHighlighter CreateHighlighter(TextView textView, ICSharpCode.AvalonEdit.Document.TextDocument document)
 {
     return(new SemanticHighlighter(_workspace, _documentId, document, _highlightingColors));
 }
        /// <inheritdoc/>
        protected override IEnumerable <NewFolding> CreateNewFoldings(
            AvalonEditDocument document, out int firstErrorOffset)
        {
            firstErrorOffset = -1;

            List <NewFolding> foldings = new List <NewFolding>();

            Stack <NewFolding> activeFoldings     = new Stack <NewFolding>();
            Stack <int>        activeIndentations = new Stack <int>();

            NewFolding activeFolding               = null;
            int        activeIndentation           = -1;
            int        endOffsetOfLastNonEmptyLine = 0;

            for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++)
            {
                var line = document.GetLineByNumber(lineNumber);

                // Determine indentation of line.
                int  offset      = line.Offset;
                int  indentation = 0;
                bool isLineEmpty = true;
                for (int i = 0; i < line.Length; i++)
                {
                    if (char.IsWhiteSpace(document.GetCharAt(offset + i)))
                    {
                        indentation++;
                    }
                    else
                    {
                        // Found the first non-white space character.
                        isLineEmpty = false;
                        break;
                    }
                }

                // Skip empty lines.
                if (isLineEmpty)
                {
                    continue;
                }

                // If the indentation is less than the previous, then we close the last active
                // folding.
                if (indentation < activeIndentation)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    activeFolding.EndOffset = endOffsetOfLastNonEmptyLine;

                    // Keep foldings which span more than one line.
                    if (document.GetLineByOffset(activeFolding.StartOffset) != document.GetLineByOffset(activeFolding.EndOffset))
                    {
                        foldings.Add(activeFolding);
                    }

                    if (activeFoldings.Count > 0)
                    {
                        activeFolding     = activeFoldings.Pop();
                        activeIndentation = activeIndentations.Pop();
                    }
                    else
                    {
                        activeFolding     = null;
                        activeIndentation = -1;
                    }

                    // Test same line again. The previous folding could also end on this line.
                    lineNumber--;
                    continue;
                }

                endOffsetOfLastNonEmptyLine = line.EndOffset;

                // If the indentation is larger than the previous indentation, we start a new
                // folding.
                if (indentation > 0 && indentation > activeIndentation)
                {
                    // Store current folding on stack.
                    if (activeFolding != null)
                    {
                        activeFoldings.Push(activeFolding);
                        activeIndentations.Push(activeIndentation);
                    }

                    activeFolding = new NewFolding {
                        StartOffset = offset + indentation,
                    };
                    activeIndentation = indentation;
                }
            }

            // Close all open foldings.
            while (activeFoldings.Count > 0)
            {
                var folding = activeFoldings.Pop();
                folding.EndOffset = endOffsetOfLastNonEmptyLine;
                foldings.Add(folding);
            }

            foldings.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset));
            return(foldings);
        }
 void SyntaxHighlighterViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
 {
     if (e.PropertyName == "Text")
         Document = new ICSharpCode.AvalonEdit.Document.TextDocument(Text);
 }