Esempio n. 1
0
        protected override int GetVariableNamePosition(IEditableTextFile file, LocalVariable var)
        {
            int begin = file.GetPositionFromLineColumn(var.Region.Start.Line, var.Region.Start.Column);
            int end   = file.GetPositionFromLineColumn(var.Region.Start.Line, var.Region.End.Column);

            if (begin == -1 || end == -1)
            {
                return(-1);
            }

            string txt = file.GetText(begin, end);

            int i = 0;             /* = txt.IndexOf ('=');
                                    * if (i == -1)
                                    * i = txt.Length;*/

            int pos = -1;

            do
            {
                i = pos = txt.IndexOf(var.Name, i);
            } while ((pos > 0 && !Char.IsLetter(file.GetCharAt(pos - 1))) &&
                     (pos + txt.Length + 1 < file.Length) && !Char.IsLetterOrDigit(file.GetCharAt(pos + txt.Length + 1))
                     );
            if (pos == -1)
            {
                return(-1);
            }

            return(begin + pos);
        }
Esempio n. 2
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));
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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 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);
        }
		protected override int GetMemberNamePosition (IEditableTextFile file, IMember member)
		{
			int begin = file.GetPositionFromLineColumn (member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
			int end = file.GetPositionFromLineColumn (member.BodyRegion.End.Line, member.BodyRegion.End.Column);
			
			if (begin == -1 || end == -1)
				return -1;
			
			string txt = file.GetText (begin, end);
			string name = member.Name;
			int len = txt.Length;
			int pos = -1;
			if (member is IField) {
				// Fields are different because multiple fields can be declared
				// in the same region and might even reference each other
				// e.g. "public int fu, bar = 1, baz = bar;"
				do {
					if ((pos = txt.IndexOf (member.Name, pos + 1)) == -1)
						return -1;
				} while (!IsMatchedField (txt, member.Name, pos));
				
				return begin + pos;
			} else if (member is IMethod) {
				if ((len = txt.IndexOf ('(')) == -1)
					return -1;
				
				if (((IMethod) member).IsConstructor)
					name = member.DeclaringType.Name;
			} else if (member is IEvent) {
				// no variables to change
			} else if (member is IProperty) {
				if (((IProperty)member).IsIndexer && (len = txt.IndexOf ('[')) == -1)
					return -1;
			} else {
				return -1;
			}
			
			if ((pos = txt.LastIndexOf (name, len)) == -1)
				return -1;
			
			return begin + pos;
		}
		protected override int GetParameterNamePosition (IEditableTextFile file, IParameter param)
		{
			IMember member = param.DeclaringMember;
			int begin = file.GetPositionFromLineColumn (member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
			int end = file.GetPositionFromLineColumn (member.BodyRegion.End.Line, member.BodyRegion.End.Column);
			
			if (begin == -1 || end == -1)
				return -1;
			
			string txt = file.GetText (begin, end);
			int open, close, i, j;
			char obrace, cbrace;
			
			if (member is IProperty) { // indexer
				obrace = '[';
				cbrace = ']';
			} else {
				obrace = '(';
				cbrace = ')';
			}
			
			if ((open = txt.IndexOf (obrace)) == -1)
				return -1;
			
			if ((close = txt.LastIndexOf (cbrace)) == -1)
				return -1;
			
			open++;
			
			while (open < close) {
				if ((i = txt.IndexOf (param.Name, open)) == -1)
					return -1;
				
				if (!Char.IsWhiteSpace (txt[i - 1]))
					return -1;
				
				j = i + param.Name.Length;
				if (j == close || Char.IsWhiteSpace (txt[j]) || txt[j] == ',')
					return begin + i;
				
				if ((open = txt.IndexOf (',', i)) == -1)
					return -1;
				
				open++;
			}
			
			return -1;
		}
		protected override int GetVariableNamePosition (IEditableTextFile file, LocalVariable var)
		{
			int begin = file.GetPositionFromLineColumn (var.Region.Start.Line, var.Region.Start.Column);
			int end = file.GetPositionFromLineColumn (var.Region.Start.Line, var.Region.End.Column);
			
			if (begin == -1 || end == -1)
				return -1;
			
			string txt = file.GetText (begin, end);
			
			int i = 0; /* = txt.IndexOf ('=');
			if (i == -1)
				i = txt.Length;*/
			
			int pos = -1;
			do {
				i = pos = txt.IndexOf (var.Name, i);
			} while ( (pos > 0 && !Char.IsLetter (file.GetCharAt (pos - 1))) &&
			          (pos + txt.Length + 1 < file.Length )&& !Char.IsLetterOrDigit (file.GetCharAt (pos + txt.Length + 1))
			         );
			if (pos == -1)
				return -1;
			
			return begin + pos;
		}
Esempio n. 9
0
		protected virtual int GetNewMethodPosition (IEditableTextFile buffer, IType cls)
		{
			cls = GetMainPart (cls);
			if (cls.MethodCount + cls.ConstructorCount == 0) {
				return GetNewPropertyPosition (buffer, cls);
				/*int pos = GetNewPropertyPosition (buffer, cls);
				int line, col;
				buffer.GetLineColumnFromPosition (pos, out line, out col);
				string ind = GetLineIndent (buffer, line);
				pos = GetNextLine (buffer, pos);
				return EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, ind, pos);*/
			} else {
				var m = cls.Members .Last ();
				
				int pos;
				if (!m.BodyRegion.IsEmpty && m.BodyRegion.End.Line > 1) {
					pos = buffer.GetPositionFromLineColumn (m.BodyRegion.End.Line, m.BodyRegion.End.Column);
					pos = GetNextLine (buffer, pos);
					pos = SkipBlankLine (buffer, pos);
				} else {
					// Abstract or P/Inboke methods don't have a body
					pos = buffer.GetPositionFromLineColumn (m.Location.Line, m.Location.Column);
					pos = GetNextLine (buffer, pos);
				}
				
//				buffer.InsertText (pos++, "\n");
				string ind = GetLineIndent (buffer, m.Location.Line);
				pos = EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, ind, pos);
				
				return pos;
			}
		}
Esempio n. 10
0
        protected override int GetMemberNamePosition(IEditableTextFile file, IMember member)
        {
            int begin = file.GetPositionFromLineColumn(member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
            int end   = file.GetPositionFromLineColumn(member.BodyRegion.End.Line, member.BodyRegion.End.Column);

            if (begin == -1 || end == -1)
            {
                return(-1);
            }

            string txt  = file.GetText(begin, end);
            string name = member.Name;
            int    len  = txt.Length;
            int    pos  = -1;

            if (member is IField)
            {
                // Fields are different because multiple fields can be declared
                // in the same region and might even reference each other
                // e.g. "public int fu, bar = 1, baz = bar;"
                do
                {
                    if ((pos = txt.IndexOf(member.Name, pos + 1)) == -1)
                    {
                        return(-1);
                    }
                } while (!IsMatchedField(txt, member.Name, pos));

                return(begin + pos);
            }
            else if (member is IMethod)
            {
                if ((len = txt.IndexOf('(')) == -1)
                {
                    return(-1);
                }

                if (((IMethod)member).IsConstructor)
                {
                    name = member.DeclaringType.Name;
                }
            }
            else if (member is IEvent)
            {
                // no variables to change
            }
            else if (member is IProperty)
            {
                if (((IProperty)member).IsIndexer && (len = txt.IndexOf('[')) == -1)
                {
                    return(-1);
                }
            }
            else
            {
                return(-1);
            }

            if ((pos = txt.LastIndexOf(name, len)) == -1)
            {
                return(-1);
            }

            return(begin + pos);
        }
Esempio n. 11
0
        protected override int GetParameterNamePosition(IEditableTextFile file, IParameter param)
        {
            IMember member = param.DeclaringMember;
            int     begin  = file.GetPositionFromLineColumn(member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
            int     end    = file.GetPositionFromLineColumn(member.BodyRegion.End.Line, member.BodyRegion.End.Column);

            if (begin == -1 || end == -1)
            {
                return(-1);
            }

            string txt = file.GetText(begin, end);
            int    open, close, i, j;
            char   obrace, cbrace;

            if (member is IProperty)               // indexer
            {
                obrace = '[';
                cbrace = ']';
            }
            else
            {
                obrace = '(';
                cbrace = ')';
            }

            if ((open = txt.IndexOf(obrace)) == -1)
            {
                return(-1);
            }

            if ((close = txt.LastIndexOf(cbrace)) == -1)
            {
                return(-1);
            }

            open++;

            while (open < close)
            {
                if ((i = txt.IndexOf(param.Name, open)) == -1)
                {
                    return(-1);
                }

                if (!Char.IsWhiteSpace(txt[i - 1]))
                {
                    return(-1);
                }

                j = i + param.Name.Length;
                if (j == close || Char.IsWhiteSpace(txt[j]) || txt[j] == ',')
                {
                    return(begin + i);
                }

                if ((open = txt.IndexOf(',', i)) == -1)
                {
                    return(-1);
                }

                open++;
            }

            return(-1);
        }
Esempio n. 12
0
		protected virtual int GetNewPropertyPosition (IEditableTextFile buffer, IType cls)
		{
			cls = GetMainPart (cls);
			if (cls.PropertyCount == 0) {
				return GetNewFieldPosition (buffer, cls);
/*				int pos = GetNewFieldPosition (buffer, cls);
				int line, col;
				buffer.GetLineColumnFromPosition (pos, out line, out col);
				string indent = GetLineIndent (buffer, line);
				pos = GetNextLine (buffer, pos);
				return EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, indent, pos);*/
			} else {
				IProperty m = cls.Properties.Last ();
				
				int pos = buffer.GetPositionFromLineColumn (m.BodyRegion.End.Line, m.BodyRegion.End.Column);
				pos = GetNextLine (buffer, pos);
				pos = SkipBlankLine (buffer, pos);
				string indent = GetLineIndent (buffer, m.Location.Line);
				return EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, indent, pos);
			}
		}
Esempio n. 13
0
        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());
            }
        }
Esempio n. 14
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);
			}
		}
Esempio n. 15
0
		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;
		}
Esempio n. 16
0
		protected string GetLineIndent (IEditableTextFile buffer, int line)
		{
			int pos = buffer.GetPositionFromLineColumn (line, 1);
			StringBuilder result = new StringBuilder ();
			while (pos < buffer.Length) {
				char ch = buffer.GetCharAt (pos);
				if (ch == ' ' || ch == '\t') {
					result.Append (ch);
					pos++;
					continue;
				}
				break;
			}
			return result.ToString ();
		}
Esempio n. 17
0
		protected virtual int GetNewEventPosition (IEditableTextFile buffer, IType cls)
		{
			cls = GetMainPart (cls);
			if (cls.EventCount == 0) {
				return GetNewMethodPosition (buffer, cls);
/*				int pos = GetNewMethodPosition (buffer, cls);
				int line, col;
				buffer.GetLineColumnFromPosition (pos, out line, out col);
				string ind = GetLineIndent (buffer, line);
				pos = GetNextLine (buffer, pos);
				return EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, ind, pos);*/
			} else {
				IEvent m = GetMainPart (cls).Events.Last ();
				
				int pos;
				if (!m.BodyRegion.IsEmpty) {
					pos = buffer.GetPositionFromLineColumn (m.BodyRegion.End.Line, m.BodyRegion.End.Column);
					pos = GetNextLine (buffer, pos);
					pos = SkipBlankLine (buffer, pos);
				} else {
					pos = buffer.GetPositionFromLineColumn (m.Location.Line, m.Location.Column);
					pos = GetNextLine (buffer, pos);
				}

//				buffer.InsertText (pos++, "\n");
				string ind = GetLineIndent (buffer, m.Location.Line);
				return EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, ind, pos);
			}
		}