public override DomLocation CompleteStatement(RefactorerContext ctx, string fileName, DomLocation caretLocation)
        {
            IEditableTextFile file = ctx.GetFile(fileName);
            int pos = file.GetPositionFromLineColumn(caretLocation.Line + 1, 1);

            StringBuilder line = new StringBuilder();
            int           lineNr = caretLocation.Line + 1, column = 1, maxColumn = 1, lastPos = pos;

            while (lineNr == caretLocation.Line + 1)
            {
                maxColumn = column;
                lastPos   = pos;
                line.Append(file.GetCharAt(pos));
                pos++;
                file.GetLineColumnFromPosition(pos, out lineNr, out column);
            }
            string trimmedline = line.ToString().Trim();
            string indent      = line.ToString().Substring(0, line.Length - line.ToString().TrimStart(' ', '\t').Length);

            if (trimmedline.EndsWith(";") || trimmedline.EndsWith("{"))
            {
                return(caretLocation);
            }
            if (trimmedline.StartsWith("if") ||
                trimmedline.StartsWith("while") ||
                trimmedline.StartsWith("switch") ||
                trimmedline.StartsWith("for") ||
                trimmedline.StartsWith("foreach"))
            {
                if (!trimmedline.EndsWith(")"))
                {
                    file.InsertText(lastPos, " () {" + Environment.NewLine + indent + TextEditorProperties.IndentString + Environment.NewLine + indent + "}");
                    caretLocation.Column = maxColumn + 1;
                }
                else
                {
                    file.InsertText(lastPos, " {" + Environment.NewLine + indent + TextEditorProperties.IndentString + Environment.NewLine + indent + "}");
                    caretLocation.Column = indent.Length + 1;
                    caretLocation.Line++;
                }
            }
            else if (trimmedline.StartsWith("do"))
            {
                file.InsertText(lastPos, " {" + Environment.NewLine + indent + TextEditorProperties.IndentString + Environment.NewLine + indent + "} while ();");
                caretLocation.Column = indent.Length + 1;
                caretLocation.Line++;
            }
            else
            {
                file.InsertText(lastPos, ";" + Environment.NewLine + indent);
                caretLocation.Column = indent.Length;
                caretLocation.Line++;
            }
            return(caretLocation);
        }
        public override int AddFoldingRegion(RefactorerContext ctx, IType cls, string regionName)
        {
            IEditableTextFile buffer = ctx.GetFile(cls.CompilationUnit.FileName);
            int    pos       = GetNewMethodPosition(buffer, cls);
            string eolMarker = Environment.NewLine;

            if (cls.SourceProject != null)
            {
                TextStylePolicy policy = cls.SourceProject.Policies.Get <TextStylePolicy> ();
                if (policy != null)
                {
                    eolMarker = policy.GetEolMarker();
                }
            }

            int line, col;

            buffer.GetLineColumnFromPosition(pos, out line, out col);

            string indent = buffer.GetText(buffer.GetPositionFromLineColumn(line, 1), pos);

            string pre  = "#region " + regionName + eolMarker;
            string post = indent + "#endregion" + eolMarker;

            buffer.InsertText(pos, pre + post);
            return(pos + pre.Length);
        }
Beispiel #3
0
        public override void PerformChange(IProgressMonitor monitor, RefactorerContext rctx)
        {
            if (rctx == null)
            {
                throw new InvalidOperationException("Refactory context not available.");
            }

            TextEditorData textEditorData = this.TextEditorData;

            if (textEditorData == null)
            {
                IEditableTextFile file = rctx.GetFile(FileName);
                if (file != null)
                {
                    if (RemovedChars > 0)
                    {
                        file.DeleteText(Offset, RemovedChars);
                    }
                    if (!string.IsNullOrEmpty(InsertedText))
                    {
                        file.InsertText(Offset, InsertedText);
                    }
                    rctx.Save();
                }
            }
            else if (textEditorData != null)
            {
                int charsInserted = textEditorData.Replace(Offset, RemovedChars, InsertedText);
                if (MoveCaretToReplace)
                {
                    textEditorData.Caret.Offset = Offset + charsInserted;
                }
            }
        }
Beispiel #4
0
        public override IClass RenameClass(RefactorerContext ctx, IClass cls, string newName)
        {
            IEditableTextFile file = ctx.GetFile(cls.Region.FileName);

            if (file == null)
            {
                return(null);
            }

            int    pos1 = file.GetPositionFromLineColumn(cls.Region.BeginLine, cls.Region.BeginColumn);
            int    pos2 = file.GetPositionFromLineColumn(cls.Region.EndLine, cls.Region.EndColumn);
            string txt  = file.GetText(pos1, pos2);

            Regex targetExp = new Regex(@"\sclass\s*(" + cls.Name + @")\s", RegexOptions.Multiline);
            Match match     = targetExp.Match(" " + txt + " ");

            if (!match.Success)
            {
                return(null);
            }

            int pos = pos1 + match.Groups [1].Index - 1;

            file.DeleteText(pos, cls.Name.Length);
            file.InsertText(pos, newName);

            return(GetGeneratedClass(ctx, file, cls));
        }
        public override void AddGlobalNamespaceImport(RefactorerContext ctx, string fileName, string nsName)
        {
            IEditableTextFile file        = ctx.GetFile(fileName);
            int            pos            = 0;
            ParsedDocument parsedDocument = parser.Parse(ctx.ParserContext, fileName, file.Text);
            StringBuilder  text           = new StringBuilder();

            if (parsedDocument.CompilationUnit != null)
            {
                IUsing lastUsing = null;
                foreach (IUsing u in parsedDocument.CompilationUnit.Usings)
                {
                    if (u.IsFromNamespace)
                    {
                        break;
                    }
                    lastUsing = u;
                }

                if (lastUsing != null)
                {
                    pos = file.GetPositionFromLineColumn(lastUsing.Region.End.Line, lastUsing.Region.End.Column);
                }
            }

            if (pos != 0)
            {
                text.AppendLine();
            }
            text.Append("using ");
            text.Append(nsName);
            text.Append(";");
            if (file is Mono.TextEditor.ITextEditorDataProvider)
            {
                Mono.TextEditor.TextEditorData data = ((Mono.TextEditor.ITextEditorDataProvider)file).GetTextEditorData();
                if (pos == 0)
                {
                    text.Append(data.EolMarker);
                }
                int caretOffset   = data.Caret.Offset;
                int insertedChars = data.Insert(pos, text.ToString());
                if (pos < caretOffset)
                {
                    data.Caret.Offset = caretOffset + insertedChars;
                }
            }
            else
            {
                if (pos == 0)
                {
                    text.AppendLine();
                }
                file.InsertText(pos, text.ToString());
            }
        }
        public virtual void Rename(string newName)
        {
            if (rctx == null)
            {
                throw new InvalidOperationException("Refactory context not available.");
            }

            IEditableTextFile file = rctx.GetFile(fileName);

            if (file != null)
            {
                Console.WriteLine("Replacing text \"{0}\" with \"{1}\" @ {2},{3}", name, newName, line, column);
                file.DeleteText(position, name.Length);
                file.InsertText(position, newName);
                rctx.Save();
            }
        }
        public override void AddLocalNamespaceImport(RefactorerContext ctx, string fileName, string nsName, DomLocation caretLocation)
        {
            IEditableTextFile file        = ctx.GetFile(fileName);
            int            pos            = 0;
            ParsedDocument parsedDocument = parser.Parse(ctx.ParserContext, fileName, file.Text);
            StringBuilder  text           = new StringBuilder();
            string         indent         = "";

            if (parsedDocument.CompilationUnit != null)
            {
                IUsing containingUsing = null;
                foreach (IUsing u in parsedDocument.CompilationUnit.Usings)
                {
                    if (u.IsFromNamespace && u.Region.Contains(caretLocation))
                    {
                        containingUsing = u;
                    }
                }

                if (containingUsing != null)
                {
                    indent = GetLineIndent(file, containingUsing.Region.Start.Line);

                    IUsing lastUsing = null;
                    foreach (IUsing u in parsedDocument.CompilationUnit.Usings)
                    {
                        if (u == containingUsing)
                        {
                            continue;
                        }
                        if (containingUsing.Region.Contains(u.Region))
                        {
                            if (u.IsFromNamespace)
                            {
                                break;
                            }
                            lastUsing = u;
                        }
                    }

                    if (lastUsing != null)
                    {
                        pos = file.GetPositionFromLineColumn(lastUsing.Region.End.Line, lastUsing.Region.End.Column);
                    }
                    else
                    {
                        pos = file.GetPositionFromLineColumn(containingUsing.ValidRegion.Start.Line, containingUsing.ValidRegion.Start.Column);
                        // search line end
                        while (pos < file.Length)
                        {
                            char ch = file.GetCharAt(pos);
                            if (ch == '\n')
                            {
                                if (file.GetCharAt(pos + 1) == '\r')
                                {
                                    pos++;
                                }
                                break;
                            }
                            else if (ch == '\r')
                            {
                                break;
                            }
                            pos++;
                        }
                    }
                }
                else
                {
                    AddGlobalNamespaceImport(ctx, fileName, nsName);
                    return;
                }
            }
            if (pos != 0)
            {
                text.AppendLine();
            }
            text.Append(indent);
            text.Append("\t");
            text.Append("using ");
            text.Append(nsName);
            text.Append(";");
            if (file is Mono.TextEditor.ITextEditorDataProvider)
            {
                Mono.TextEditor.TextEditorData data = ((Mono.TextEditor.ITextEditorDataProvider)file).GetTextEditorData();
                if (pos == 0)
                {
                    text.Append(data.EolMarker);
                }
                int caretOffset   = data.Caret.Offset;
                int insertedChars = data.Insert(pos, text.ToString());
                if (pos < caretOffset)
                {
                    data.Caret.Offset = caretOffset + insertedChars;
                }
            }
            else
            {
                if (pos == 0)
                {
                    text.AppendLine();
                }
                file.InsertText(pos, text.ToString());
            }
        }
		protected int EnsurePositionIsNotInRegionsAndIndented (Project p, IEditableTextFile buffer, string indent, int position)
		{
			ParsedDocument doc = ProjectDomService.Parse (p, buffer.Name, delegate () { return buffer.Text; });
			int line, column;
			buffer.GetLineColumnFromPosition (position, out line, out column);
			
			foreach (FoldingRegion region in doc.AdditionalFolds) {
				if (region.Region.Contains (line, column)) {
					line = region.Region.End.Line + 1;
					column = 1;
				}
			}
			
			int result = buffer.GetPositionFromLineColumn (line, column);
			
			if (column != 1) {
				string eolMarker = Environment.NewLine;
				buffer.InsertText (result, eolMarker);
				result += eolMarker.Length;
			}
			
			buffer.InsertText (result, indent);
			result += indent.Length;
			
			return result;
		}
		protected void AddMembersAtPosition (RefactorerContext ctx, IType cls, IEnumerable<CodeTypeMember> members, 
		                                     IEditableTextFile buffer, int pos)
		{
			int line, col;
			buffer.GetLineColumnFromPosition (pos, out line, out col);
			
			string indent = GetLineIndent (buffer, line);
			
			StringBuilder generatedString = new StringBuilder ();
			bool isFirst = true;
			foreach (CodeTypeMember member in members) {
				if (generatedString.Length > 0) {
					generatedString.AppendLine ();
				}
				generatedString.Append (Indent (GenerateCodeFromMember (member), indent, isFirst));
				isFirst = false;
			}
			
			// remove last new line + indent
			generatedString.Length -= indent.Length + Environment.NewLine.Length;
			// remove indent from last generated code member
			generatedString.Length -= indent.Length;
			if (buffer.GetCharAt (pos) == '\n')
				pos++;
			buffer.InsertText (pos, generatedString.ToString ());
		}
Beispiel #10
0
		protected virtual int GetNewFieldPosition (IEditableTextFile buffer, IType cls)
		{
			cls = GetMainPart (cls);
			if (cls.FieldCount == 0) {
				int sp = buffer.GetPositionFromLineColumn (cls.BodyRegion.Start.Line, cls.BodyRegion.Start.Column);
				int ep = buffer.GetPositionFromLineColumn (cls.BodyRegion.End.Line, cls.BodyRegion.End.Column);
				string s = buffer.GetText (sp, ep);
				int i = s.IndexOf ('{');
				if (i == -1) return -1;
				string ind = GetLineIndent (buffer, cls.BodyRegion.Start.Line) ;
				int pos;
				if (cls.BodyRegion.Start.Line == cls.BodyRegion.End.Line) {
					buffer.InsertText (sp + i + 1, "\n" + ind);
					pos = sp + i + 2;
				} else  {
					pos = GetNextLine (buffer, sp + i + 1);
//					buffer.InsertText (pos, ind + "\n");
//					pos += ind.Length + 1;
				}
				return EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, ind + "\t", pos);
			} else {
				IField f = cls.Fields.Last ();
				int pos = buffer.GetPositionFromLineColumn (f.Location.Line, f.Location.Column);
				string ind = GetLineIndent (buffer, f.Location.Line);
				if (cls.BodyRegion.Start.Line == cls.BodyRegion.End.Line) {
					int sp = buffer.GetPositionFromLineColumn (cls.BodyRegion.Start.Line, cls.BodyRegion.Start.Column);
					int ep = buffer.GetPositionFromLineColumn (cls.BodyRegion.End.Line, cls.BodyRegion.End.Column);
					string s = buffer.GetText (sp, ep);
					int i = s.IndexOf ('}');
					if (i == -1) return -1;
//					buffer.InsertText (sp + i, "\n" + ind + "\t\n" + ind);
					pos = sp + i + ind.Length + 2;
				} else {
					pos = GetNextLine (buffer, pos);
				}
//				buffer.InsertText (pos, ind);
				return EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, ind, pos);
			}
		}