Ejemplo n.º 1
0
        public void Execute()
        {
            IWpfTextView textView = GetTextView();

            ITextSnapshot snapshot = textView.TextSnapshot;

            if (snapshot != snapshot.TextBuffer.CurrentSnapshot)
            {
                return;
            }

            if (!textView.Selection.IsEmpty)
            {
                return;
            }

            int caretGlobalPos          = textView.Caret.Position.BufferPosition.Position;
            int caretLineGlobalStartPos = textView.GetTextViewLineContainingBufferPosition(textView.Caret.Position.BufferPosition).Start.Position;
            int initialCaretXPosition   = caretGlobalPos - caretLineGlobalStartPos;

            int startLineNumber = snapshot.GetLineNumberFromPosition(textView.Caret.Position.BufferPosition);

            string lineText = snapshot.GetLineFromLineNumber(startLineNumber).GetText();

            var refactor = new CSharpRefactor();

            string replacementCode = "";

            CSharpRefactor.FldInfo info = refactor.ProbeAsField(lineText);

            if (!info.IsValid)
            {
                return;
            }
            else
            {
                replacementCode = refactor.EmittFullProperty(info);
            }

            if (replacementCode == "")
            {
                return;
            }

            //double initialStartPosition =  textView.Caret.Left;

            //replace existing property definition
            ITextEdit         edit        = snapshot.TextBuffer.CreateEdit();
            ITextSnapshotLine currentLine = snapshot.GetLineFromLineNumber(startLineNumber);

            edit.Delete(currentLine.Start.Position, currentLine.LengthIncludingLineBreak);
            edit.Insert(snapshot.GetLineFromLineNumber(startLineNumber).Start.Position, replacementCode);
            edit.Apply();

            int caretLineOffset    = 2; //shift caret to the property definition
            ITextSnapshotLine line = textView.TextSnapshot.GetLineFromLineNumber(startLineNumber + caretLineOffset);

            SnapshotPoint point = new SnapshotPoint(line.Snapshot, line.Start.Position + initialCaretXPosition);

            textView.Caret.MoveTo(point);
        }
Ejemplo n.º 2
0
        public void Execute()
        {
            IWpfTextView textView = GetTextView();

            ITextSnapshot snapshot = textView.TextSnapshot;

            if (snapshot != snapshot.TextBuffer.CurrentSnapshot)
            {
                return;
            }

            if (!textView.Selection.IsEmpty)
            {
                return;
            }

            int caretGlobalPos          = textView.Caret.Position.BufferPosition.Position;
            int caretLineGlobalStartPos = textView.GetTextViewLineContainingBufferPosition(textView.Caret.Position.BufferPosition).Start.Position;
            int initialCaretXPosition   = caretGlobalPos - caretLineGlobalStartPos;

            int startLineNumber   = snapshot.GetLineNumberFromPosition(textView.Caret.Position.BufferPosition);
            int currentLineNumber = startLineNumber;

            var refactor = new CSharpRefactor();

            int l = currentLineNumber;

            string originalCode = refactor.AgregateCodeBlock(() =>
            {
                try
                {
                    return(snapshot.GetLineFromLineNumber(l++).GetText());
                }
                catch
                {
                    return(null);
                }
            });

            currentLineNumber += originalCode.TrimEnd().Split('\n').Length;
            string replacementCode = "";

            CSharpRefactor.PropInfo info = refactor.ProbeAsProperty(originalCode);

            string fieldDeclarationToDelete = null;
            int    newCaretLineOffset       = 0;

            if (!info.IsValid)
            {
                return;
            }
            else
            {
                if (info.IsAuto)
                {
                    newCaretLineOffset = 2; //FullProperty has extra two lines (field declaration)
                    //on top of the property definition
                    replacementCode = refactor.EmittFullProperty(info);
                }
                else if (!info.IsAuto)
                {
                    //look only above and only a primitive declarations without initialization
                    fieldDeclarationToDelete = info.AccessModifiers.Split(' ').LastOrDefault() + " " +
                                               char.ToLower(info.Name[0]) + info.Name.Substring(1) +
                                               ";";

                    //if (info.IsCompleteAndSimple)
                    replacementCode = refactor.EmittAutoProperty(info);
                    //else
                    //    WriteToOutput("Error: Only primitive get/set properties can be \"collapsed\".");
                }
            }

            if (replacementCode == "")
            {
                return;
            }

            //double initialStartPosition =  textView.Caret.Left;

            //replace existing property definition
            ITextEdit edit = snapshot.TextBuffer.CreateEdit();

            for (int i = startLineNumber; i < currentLineNumber; i++)
            {
                ITextSnapshotLine currentLine = snapshot.GetLineFromLineNumber(i);
                edit.Delete(currentLine.Start.Position, currentLine.LengthIncludingLineBreak);
            }


            var fieldLinesToDelete = new List <int>();
            var initialValue       = "";

            //search for field in the above code
            for (int i = startLineNumber; i >= 0 && !fieldLinesToDelete.Any(); i--)
            {
                ITextSnapshotLine currentLine = snapshot.GetLineFromLineNumber(i);
                string            lineText    = currentLine.GetText();
                foreach (string pattern in info.FullPropFieldExpectatedPatterns)
                {
                    if (lineText.Trim().ToLower().StartsWith(pattern))
                    {
                        fieldLinesToDelete.Add(i);

                        //int count = 0;
                        initialValue = lineText.Trim()
                                       .TrimEnd()
                                       .Substring(pattern.Length)
                                       .TrimStart();

                        if (initialValue.StartsWith("=") && initialValue.Contains(";"))
                        {
                            initialValue = " " + initialValue;
                        }
                        else
                        {
                            initialValue = "";
                        }

                        var nextLine     = snapshot.GetLineFromLineNumber(i + 1);
                        var nextLineText = nextLine.GetText();
                        if (string.IsNullOrWhiteSpace(nextLineText))
                        {
                            fieldLinesToDelete.Add(i + 1);
                        }
                        break;
                    }
                }
            }

            replacementCode = replacementCode.TrimEnd() + initialValue + Environment.NewLine;

            edit.Insert(snapshot.GetLineFromLineNumber(startLineNumber).Start.Position, replacementCode);
            edit.Apply();

            ITextSnapshotLine line  = textView.TextSnapshot.GetLineFromLineNumber(startLineNumber + newCaretLineOffset);
            SnapshotPoint     point = new SnapshotPoint(line.Snapshot, line.Start.Position + initialCaretXPosition);

            textView.Caret.MoveTo(point);


            if (fieldLinesToDelete.Any())
            {
                ITextEdit edit1 = snapshot.TextBuffer.CreateEdit();
                foreach (int i in fieldLinesToDelete)
                {
                    ITextSnapshotLine currentLine = snapshot.GetLineFromLineNumber(i);
                    edit1.Delete(currentLine.Start.Position, currentLine.LengthIncludingLineBreak);
                }
                edit1.Apply();
            }
        }