Exemplo 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);
        }
        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 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;
		}
Exemplo n.º 4
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());
            }
        }
Exemplo n.º 5
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 ());
		}
Exemplo n.º 6
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 ();
		}
Exemplo n.º 7
0
		protected virtual int GetNextLine (IEditableTextFile buffer, int pos)
		{
			if (pos < 0)
				return 0;
			while (pos < buffer.Length) {
				char ch = buffer.GetCharAt (pos);
				switch (ch) {
				case '\n':
					return pos + 1;
				case '\r':
					if (pos + 1 < buffer.Length && buffer.GetCharAt (pos + 1) == '\n')
						pos++;
					return pos + 1;
/*				case ' ':
				case '\t':
					pos++;
					break;*/
				default:
					pos++;
					continue;
				}
			}
			return pos;
		}
Exemplo n.º 8
0
		protected virtual int SkipBlankLine (IEditableTextFile buffer, int pos)
		{
			int i = pos;
			while (i < buffer.Length) {
				char ch = buffer.GetCharAt (i);
				switch (ch) {
				case '\n':
					return i + 1;
				case '\r':
					if (i + 1 < buffer.Length && buffer.GetCharAt (i + 1) == '\n')
						i++;
					return i + 1;
				case ' ':
				case '\t':
					i++;
					break;
				default:
					return pos;
				}
			}
			return pos;
		}