public void RegisterSourceFile(IIntellisenseControl intellisenseControl, ICompletionAssistant completionAssistant, TextEditor editor, ISourceFile file, TextDocument textDocument)
        {
            CSharpDataAssociation association = null;

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

            association          = new CSharpDataAssociation(textDocument);
            association.Solution = file.Project.Solution as OmniSharpSolution; // CanHandle has checked this.

            dataAssociations.Add(file, association);

            association.KeyUpHandler = (sender, e) =>
            {
                if (editor.TextDocument == textDocument)
                {
                    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;
                    }
                }
            };

            editor.AddHandler(InputElement.KeyUpEvent, association.KeyUpHandler, RoutingStrategies.Tunnel);
        }
Beispiel #2
0
        private void ReplaceSelectionWithNewLine()
        {
            string newLine = TextUtilities.GetNewLineFromDocument(Document, Caret.Line);

            using (Document.RunUpdate()) {
                ReplaceSelectionWithText(newLine);
                if (IndentationStrategy != null)
                {
                    DocumentLine line      = Document.GetLineByNumber(Caret.Line);
                    ISegment[]   deletable = GetDeletableSegments(line);
                    if (deletable.Length == 1 && deletable[0].Offset == line.Offset && deletable[0].Length == line.Length)
                    {
                        // use indentation strategy only if the line is not read-only
                        IndentationStrategy.IndentLine(Document, line);
                    }
                }
            }
        }
Beispiel #3
0
        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;
        }