コード例 #1
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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
		protected override DomRegion GetMemberBounds (IEditableTextFile file, IMember member)
		{
			if (!(member is IField))
				return base.GetMemberBounds (file, member);
			
			// The idea here is that it is common to declare multiple fields in the same
			// statement, like so:
			//
			// public int fu, bar, baz;
			//
			// If @member is "bar", then we want to return the region containing:
			//
			// ", bar"
			//
			// so that when our caller uses this region to delete the text declaring @member,
			// it won't also delete the text declaring the other fields in this same statement.
			
			IType klass = member.DeclaringType;
			IField field = (IField) member;
			IField kfield = null, lastField = null, nextField = null;
			int lineBegin, lineEnd;
			int colBegin, colEnd;
			int pos;
			
			// find the offset of the field
			foreach (IField f in klass.Fields) {
				if (kfield != null) {
					nextField = f;
					break;
				}
				if (f.Name == field.Name) {
					kfield = f;
					continue;
				}
				lastField = f;
			}
			
			if (kfield != null && lastField.Location.CompareTo (field.Location) == 0) {
				// Field has other fields declared before it in the same statement
				pos = GetMemberNamePosition (file, member);
				
				// seek backward for declaration separator
				while (file.Text[pos] != ',')
					pos--;
				
				// eat up unneeded lwsp
				while (Char.IsWhiteSpace (file.Text[pos]))
					pos--;
				
				file.GetLineColumnFromPosition (pos, out lineBegin, out colBegin);
				
				if (nextField != null  && nextField.Location.CompareTo (field.Location) == 0) {
					// Field also has other fields declared after it in the same statement
					pos = GetMemberNamePosition (file, nextField);
					
					// seek backward for declaration separator
					while (file.Text[pos] != ',')
						pos--;
					
					// eat up unneeded lwsp
					while (Char.IsWhiteSpace (file.Text[pos]))
						pos--;
					
					file.GetLineColumnFromPosition (pos, out lineEnd, out colEnd);
				} else {
					// No fields after this...
					colEnd = field.BodyRegion.End.Column - 1;  // don't include the ';'
					lineEnd = field.BodyRegion.End.Line;
				}
			} else if (nextField != null  && nextField.Location.CompareTo (field.Location) == 0) {
				// Field has other fields declared after it in the same statement
				pos = GetMemberNamePosition (file, member);
				file.GetLineColumnFromPosition (pos, out lineBegin, out colBegin);
				pos = GetMemberNamePosition (file, nextField);
				file.GetLineColumnFromPosition (pos, out lineEnd, out colEnd);
			} else {
				// Field is declared in a statement by itself
				
				// fall back to default implementation
				return base.GetMemberBounds (file, member);
			}
			
			return new DomRegion (lineBegin, colBegin, lineEnd, colEnd);
		}
コード例 #4
0
        protected override DomRegion GetMemberBounds(IEditableTextFile file, IMember member)
        {
            if (!(member is IField))
            {
                return(base.GetMemberBounds(file, member));
            }

            // The idea here is that it is common to declare multiple fields in the same
            // statement, like so:
            //
            // public int fu, bar, baz;
            //
            // If @member is "bar", then we want to return the region containing:
            //
            // ", bar"
            //
            // so that when our caller uses this region to delete the text declaring @member,
            // it won't also delete the text declaring the other fields in this same statement.

            IType  klass = member.DeclaringType;
            IField field = (IField)member;
            IField kfield = null, lastField = null, nextField = null;
            int    lineBegin, lineEnd;
            int    colBegin, colEnd;
            int    pos;

            // find the offset of the field
            foreach (IField f in klass.Fields)
            {
                if (kfield != null)
                {
                    nextField = f;
                    break;
                }
                if (f.Name == field.Name)
                {
                    kfield = f;
                    continue;
                }
                lastField = f;
            }

            if (kfield != null && lastField.Location.CompareTo(field.Location) == 0)
            {
                // Field has other fields declared before it in the same statement
                pos = GetMemberNamePosition(file, member);

                // seek backward for declaration separator
                while (file.Text[pos] != ',')
                {
                    pos--;
                }

                // eat up unneeded lwsp
                while (Char.IsWhiteSpace(file.Text[pos]))
                {
                    pos--;
                }

                file.GetLineColumnFromPosition(pos, out lineBegin, out colBegin);

                if (nextField != null && nextField.Location.CompareTo(field.Location) == 0)
                {
                    // Field also has other fields declared after it in the same statement
                    pos = GetMemberNamePosition(file, nextField);

                    // seek backward for declaration separator
                    while (file.Text[pos] != ',')
                    {
                        pos--;
                    }

                    // eat up unneeded lwsp
                    while (Char.IsWhiteSpace(file.Text[pos]))
                    {
                        pos--;
                    }

                    file.GetLineColumnFromPosition(pos, out lineEnd, out colEnd);
                }
                else
                {
                    // No fields after this...
                    colEnd  = field.BodyRegion.End.Column - 1;                     // don't include the ';'
                    lineEnd = field.BodyRegion.End.Line;
                }
            }
            else if (nextField != null && nextField.Location.CompareTo(field.Location) == 0)
            {
                // Field has other fields declared after it in the same statement
                pos = GetMemberNamePosition(file, member);
                file.GetLineColumnFromPosition(pos, out lineBegin, out colBegin);
                pos = GetMemberNamePosition(file, nextField);
                file.GetLineColumnFromPosition(pos, out lineEnd, out colEnd);
            }
            else
            {
                // Field is declared in a statement by itself

                // fall back to default implementation
                return(base.GetMemberBounds(file, member));
            }

            return(new DomRegion(lineBegin, colBegin, lineEnd, colEnd));
        }
コード例 #5
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;
		}
コード例 #6
0
		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 ());
		}