Exemplo n.º 1
0
        private CPlusPlusDataAssociation GetAssociatedData(ISourceFile sourceFile)
        {
            CPlusPlusDataAssociation result = null;

            dataAssociations.TryGetValue(sourceFile, out result);

            return(result);
        }
Exemplo n.º 2
0
        public void RegisterSourceFile(AvaloniaEdit.TextEditor editor, ISourceFile file, TextDocument doc)
        {
            CPlusPlusDataAssociation association = null;

            if (dataAssociations.TryGetValue(file, out association))
            {
                throw new Exception("Source file already registered with language service.");
            }

            IndentationStrategy = new CSharpIndentationStrategy(editor.Options);

            association = new CPlusPlusDataAssociation(doc);
            dataAssociations.Add(file, association);

            association.TextInputHandler = (sender, e) =>
            {
                if (editor.Document == doc)
                {
                    editor.BeginChange();
                    OpenBracket(editor, editor.Document, e.Text);
                    CloseBracket(editor, editor.Document, e.Text);

                    switch (e.Text)
                    {
                    case "}":
                    case ";":
                        editor.CaretOffset = Format(editor.Document, 0, (uint)editor.Document.TextLength, editor.CaretOffset);
                        break;

                    case "{":
                        var lineCount = editor.Document.LineCount;
                        var offset    = Format(editor.Document, 0, (uint)editor.Document.TextLength, editor.CaretOffset);

                        // suggests clang format didnt do anything, so we can assume not moving to new line.
                        if (lineCount != editor.Document.LineCount)
                        {
                            if (offset <= editor.Document.TextLength)
                            {
                                var newLine = editor.Document.GetLineByOffset(offset);
                                editor.CaretOffset = newLine.PreviousLine.EndOffset;
                            }
                        }
                        else
                        {
                            editor.CaretOffset = offset;
                        }
                        break;
                    }

                    editor.EndChange();
                }
            };

            editor.TextArea.TextEntered += association.TextInputHandler;
        }
        private CPlusPlusDataAssociation GetAssociatedData(ISourceFile sourceFile)
        {
            CPlusPlusDataAssociation result = null;

            if (!dataAssociations.TryGetValue(sourceFile, out result))
            {
                throw new Exception("Tried to parse file that has not been registered with the language service.");
            }

            return(result);
        }
        public void RegisterSourceFile(IIntellisenseControl intellisense, ICompletionAssistant completionAssistant,
                                       TextEditor.TextEditor editor, ISourceFile file, TextDocument doc)
        {
            CPlusPlusDataAssociation association = null;

            if (dataAssociations.TryGetValue(file, out association))
            {
                throw new Exception("Source file already registered with language service.");
            }

            association = new CPlusPlusDataAssociation(doc);
            dataAssociations.Add(file, association);

            association.IntellisenseManager = new CPlusPlusIntellisenseManager(this, intellisense, completionAssistant, file, editor);

            association.TunneledKeyUpHandler = async(sender, e) =>
            {
                await intellisenseJobRunner.InvokeAsync(() => { association.IntellisenseManager.OnKeyUp(e).Wait(); });
            };

            association.TunneledKeyDownHandler = async(sender, e) =>
            {
                association.IntellisenseManager.OnKeyDown(e);

                await intellisenseJobRunner.InvokeAsync(() => { association.IntellisenseManager.CompleteOnKeyDown(e).Wait(); });
            };

            association.KeyUpHandler = (sender, e) =>
            {
                if (editor.TextDocument == doc)
                {
                    switch (e.Key)
                    {
                    case Key.Return:
                    {
                        if (editor.CaretIndex >= 0 && editor.CaretIndex < editor.TextDocument.TextLength)
                        {
                            if (editor.TextDocument.GetCharAt(editor.CaretIndex) == '}')
                            {
                                editor.TextDocument.Insert(editor.CaretIndex, Environment.NewLine);
                                editor.CaretIndex--;

                                var currentLine = editor.TextDocument.GetLineByOffset(editor.CaretIndex);

                                editor.CaretIndex = IndentationStrategy.IndentLine(editor.TextDocument, currentLine, editor.CaretIndex);
                                editor.CaretIndex = IndentationStrategy.IndentLine(editor.TextDocument, currentLine.NextLine.NextLine,
                                                                                   editor.CaretIndex);
                                editor.CaretIndex = IndentationStrategy.IndentLine(editor.TextDocument, currentLine.NextLine, editor.CaretIndex);
                            }

                            var newCaret = IndentationStrategy.IndentLine(editor.TextDocument,
                                                                          editor.TextDocument.GetLineByOffset(editor.CaretIndex), editor.CaretIndex);

                            editor.CaretIndex = newCaret;
                        }
                    }
                    break;
                    }
                }
            };

            association.TextInputHandler = (sender, e) =>
            {
                if (editor.TextDocument == doc)
                {
                    OpenBracket(editor, editor.TextDocument, e.Text);
                    CloseBracket(editor, editor.TextDocument, e.Text);

                    switch (e.Text)
                    {
                    case "}":
                    case ";":
                        editor.CaretIndex = Format(editor.TextDocument, 0, (uint)editor.TextDocument.TextLength, editor.CaretIndex);
                        break;

                    case "{":
                        var lineCount = editor.TextDocument.LineCount;
                        var offset    = Format(editor.TextDocument, 0, (uint)editor.TextDocument.TextLength, editor.CaretIndex);

                        // suggests clang format didnt do anything, so we can assume not moving to new line.
                        if (lineCount != editor.TextDocument.LineCount)
                        {
                            if (offset <= editor.TextDocument.TextLength)
                            {
                                var newLine = editor.TextDocument.GetLineByOffset(offset);
                                editor.CaretIndex = newLine.PreviousLine.EndOffset;
                            }
                        }
                        else
                        {
                            editor.CaretIndex = offset;
                        }
                        break;
                    }
                }
            };

            editor.AddHandler(InputElement.KeyDownEvent, association.TunneledKeyDownHandler, RoutingStrategies.Tunnel);
            editor.AddHandler(InputElement.KeyUpEvent, association.TunneledKeyUpHandler, RoutingStrategies.Tunnel);
            editor.AddHandler(InputElement.KeyUpEvent, association.KeyUpHandler, RoutingStrategies.Tunnel);

            editor.TextInput += association.TextInputHandler;
        }
        public void RegisterSourceFile(IIntellisenseControl intellisense, ICompletionAssistant completionAssistant,
            TextEditor.TextEditor editor, ISourceFile file, TextDocument doc)
        {
            CPlusPlusDataAssociation association = null;

            if (dataAssociations.TryGetValue(file, out association))
            {
                throw new Exception("Source file already registered with language service.");
            }

            association = new CPlusPlusDataAssociation(doc);
            dataAssociations.Add(file, association);

            association.KeyUpHandler = (sender, e) =>
            {
                if (editor.TextDocument == doc)
                {
                    switch (e.Key)
                    {
                        case Key.Return:
                            {
                                if (editor.CaretIndex >= 0 && editor.CaretIndex < editor.TextDocument.TextLength)
                                {
                                    if (editor.TextDocument.GetCharAt(editor.CaretIndex) == '}')
                                    {
                                        editor.TextDocument.Insert(editor.CaretIndex, Environment.NewLine);
                                        editor.CaretIndex--;

                                        var currentLine = editor.TextDocument.GetLineByOffset(editor.CaretIndex);

                                        editor.CaretIndex = IndentationStrategy.IndentLine(editor.TextDocument, currentLine, editor.CaretIndex);
                                        editor.CaretIndex = IndentationStrategy.IndentLine(editor.TextDocument, currentLine.NextLine.NextLine,
                                            editor.CaretIndex);
                                        editor.CaretIndex = IndentationStrategy.IndentLine(editor.TextDocument, currentLine.NextLine, editor.CaretIndex);
                                    }

                                    var newCaret = IndentationStrategy.IndentLine(editor.TextDocument,
                                        editor.TextDocument.GetLineByOffset(editor.CaretIndex), editor.CaretIndex);

                                    editor.CaretIndex = newCaret;
                                }
                            }
                            break;
                    }
                }
            };

            association.TextInputHandler = (sender, e) =>
            {
                if (editor.TextDocument == doc)
                {
                    OpenBracket(editor, editor.TextDocument, e.Text);
                    CloseBracket(editor, editor.TextDocument, e.Text);

                    switch (e.Text)
                    {
                        case "}":
                        case ";":
                            editor.CaretIndex = Format(editor.TextDocument, 0, (uint)editor.TextDocument.TextLength, editor.CaretIndex);
                            break;

                        case "{":
                            var lineCount = editor.TextDocument.LineCount;
                            var offset = Format(editor.TextDocument, 0, (uint)editor.TextDocument.TextLength, editor.CaretIndex);

                            // suggests clang format didnt do anything, so we can assume not moving to new line.
                            if (lineCount != editor.TextDocument.LineCount)
                            {
                                if (offset <= editor.TextDocument.TextLength)
                                {
                                    var newLine = editor.TextDocument.GetLineByOffset(offset);
                                    editor.CaretIndex = newLine.PreviousLine.EndOffset;
                                }
                            }
                            else
                            {
                                editor.CaretIndex = offset;
                            }
                            break;
                    }
                }
            };

            editor.AddHandler(InputElement.KeyUpEvent, association.KeyUpHandler, RoutingStrategies.Tunnel);

            editor.TextInput += association.TextInputHandler;
        }
        public void RegisterSourceFile(IEditor editor)
        {
            if (clangAccessJobRunner == null)
            {
                clangAccessJobRunner = new JobRunner();

                Task.Factory.StartNew(() => { clangAccessJobRunner.RunLoop(new CancellationToken()); });
            }

            if (dataAssociations.TryGetValue(editor.SourceFile, out CPlusPlusDataAssociation association))
            {
                throw new Exception("Source file already registered with language service.");
            }

            IndentationStrategy = new CSharpIndentationStrategy(new AvaloniaEdit.TextEditorOptions {
                ConvertTabsToSpaces = true
            });

            association = new CPlusPlusDataAssociation();
            dataAssociations.Add(editor.SourceFile, association);

            association.TextInputHandler = (sender, e) =>
            {
                switch (e.Text)
                {
                case "}":
                case ";":
                    editor.IndentLine(editor.Line);
                    break;

                case "{":
                    if (IndentationStrategy != null)
                    {
                        editor.IndentLine(editor.Line);
                    }
                    break;
                }

                OpenBracket(editor, editor.Document, e.Text);
                CloseBracket(editor, editor.Document, e.Text);
            };

            association.BeforeTextInputHandler = (sender, e) =>
            {
                switch (e.Text)
                {
                case "\n":
                case "\r\n":
                    var nextChar = ' ';

                    if (editor.CaretOffset != editor.Document.TextLength)
                    {
                        nextChar = editor.Document.GetCharAt(editor.CaretOffset);
                    }

                    if (nextChar == '}')
                    {
                        var newline = "\r\n";     // TextUtilities.GetNewLineFromDocument(editor.Document, editor.TextArea.Caret.Line);
                        editor.Document.Insert(editor.CaretOffset, newline);

                        editor.Document.TrimTrailingWhiteSpace(editor.Line - 1);

                        editor.IndentLine(editor.Line);

                        editor.CaretOffset -= newline.Length;
                    }
                    break;
                }
            };

            editor.TextEntered  += association.TextInputHandler;
            editor.TextEntering += association.BeforeTextInputHandler;
        }