void CreateDocumentLoader()
 {
     document     = MockRepository.GenerateStub <IRefactoringDocument>();
     documentView = MockRepository.GenerateStub <IRefactoringDocumentView>();
     documentView.Stub(view => view.RefactoringDocument).Return(document);
     documentLoader = MockRepository.GenerateStub <IDocumentLoader>();
 }
Exemple #2
0
        /// <summary>
        /// Generates code for <paramref name="nodes"/> and inserts it into <paramref name="document"/>
        /// after the line <paramref name="insertLine"/>.
        /// </summary>
        protected void InsertCodeAfter(int insertLine, IRefactoringDocument document, string indentation, bool startWithEmptyLine, params AbstractNode[] nodes)
        {
            StringBuilder b = new StringBuilder();

            for (int i = 0; i < nodes.Length; i++)
            {
                if (options.EmptyLinesBetweenMembers)
                {
                    if (startWithEmptyLine || i > 0)
                    {
                        b.AppendLine(indentation);
                    }
                }
                b.Append(GenerateCode(nodes[i], indentation));
            }
            if (insertLine < document.TotalNumberOfLines)
            {
                IRefactoringDocumentLine lineSegment = document.GetLine(insertLine + 1);
                document.Insert(lineSegment.Offset, b.ToString());
            }
            else
            {
                b.Insert(0, Environment.NewLine);
                document.Insert(document.TextLength, b.ToString());
            }
        }
Exemple #3
0
        public virtual void CreateChangedEvent(IProperty property, IRefactoringDocument document)
        {
            ClassFinder      targetContext = new ClassFinder(property);
            string           name          = property.Name + "Changed";
            EventDeclaration ed            = new EventDeclaration {
                TypeReference = new TypeReference("EventHandler"),
                Name          = name,
                Modifier      = ConvertModifier(property.Modifiers & (ModifierEnum.VisibilityMask | ModifierEnum.Static), targetContext),
            };

            InsertCodeAfter(property, document, ed);

            List <Expression> arguments = new List <Expression>(2);

            if (property.IsStatic)
            {
                arguments.Add(new PrimitiveExpression(null, "null"));
            }
            else
            {
                arguments.Add(new ThisReferenceExpression());
            }
            arguments.Add(new IdentifierExpression("EventArgs").Member("Empty"));
            InsertCodeAtEnd(property.SetterRegion, document,
                            new RaiseEventStatement(name, arguments));
        }
Exemple #4
0
        public virtual void ImplementInterface(IReturnType interf, IRefactoringDocument document, bool explicitImpl, IClass targetClass)
        {
            List <AbstractNode> nodes = new List <AbstractNode>();

            ImplementInterface(nodes, interf, explicitImpl, targetClass);
            InsertCodeAtEnd(targetClass.Region, document, nodes.ToArray());
        }
		void CreateDocumentLoader()
		{
			document = MockRepository.GenerateStub<IRefactoringDocument>();
			documentView = MockRepository.GenerateStub<IRefactoringDocumentView>();
			documentView.Stub(view => view.RefactoringDocument).Return(document);
			documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
		}
		public void Init()
		{
			helper = new ClassHelper();
			document = MockRepository.GenerateStub<IRefactoringDocument>();
			documentView = MockRepository.GenerateStub<IRefactoringDocumentView>();
			documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
			fakeCodeGenerator = helper.CompilationUnitHelper.FakeCodeGenerator;
		}
 public void Init()
 {
     helper            = new ClassHelper();
     document          = MockRepository.GenerateStub <IRefactoringDocument>();
     documentView      = MockRepository.GenerateStub <IRefactoringDocumentView>();
     documentLoader    = MockRepository.GenerateStub <IDocumentLoader>();
     fakeCodeGenerator = helper.CompilationUnitHelper.FakeCodeGenerator;
 }
Exemple #8
0
		/// <summary>
		/// Ensure that code is inserted correctly in {} code blocks - SD2-1180
		/// </summary>
		public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
		{
			string beginLineIndentation = GetIndentation(document, region.BeginLine);
			int insertionLine = region.EndLine - 1;
			
			IRefactoringDocumentLine endLine = document.GetLine(region.EndLine);
			string endLineText = endLine.Text;
			int originalPos = region.EndColumn - 2; // -1 for column coordinate => offset, -1 because EndColumn is after the '}'
			int pos = originalPos;
			if (pos < 0 || pos >= endLineText.Length || endLineText[pos] != '}') {
				LoggingService.Warn("CSharpCodeGenerator.InsertCodeAtEnd: position is invalid (not pointing to '}')"
				                    + " endLineText=" + endLineText + ", pos=" + pos);
			} else {
				for (pos--; pos >= 0; pos--) {
					if (!char.IsWhiteSpace(endLineText[pos])) {
						// range before '}' is not empty: we cannot simply insert in the line before the '}', so
						// 
						pos++; // set pos to first whitespace character / the '{' character
						if (pos < originalPos) {
							// remove whitespace between last non-white character and the '}'
							document.Remove(endLine.Offset + pos, originalPos - pos);
						}
						// insert newline and same indentation as used in beginLine before the '}'
						document.Insert(endLine.Offset + pos, Environment.NewLine + beginLineIndentation);
						insertionLine++;
						
						pos = region.BeginColumn - 1;
						if (region.BeginLine == region.EndLine && pos >= 1 && pos < endLineText.Length) {
							// The whole block was in on a single line, e.g. "get { return field; }".
							// Insert an additional newline after the '{'.
							
							originalPos = pos = endLineText.IndexOf('{', pos);
							if (pos >= 0 && pos < region.EndColumn - 1) {
								// find next non-whitespace after originalPos
								originalPos++; // point to insertion position for newline after {
								for (pos++; pos < endLineText.Length; pos++) {
									if (!char.IsWhiteSpace(endLineText[pos])) {
										// remove all between originalPos and pos
										if (originalPos < pos) {
											document.Remove(endLine.Offset + originalPos, pos - originalPos);
										}
										document.Insert(endLine.Offset + originalPos, Environment.NewLine + beginLineIndentation + '\t');
										insertionLine++;
										break;
									}
								}
							}
						}
						break;
					}
				}
			}
			InsertCodeAfter(insertionLine, document, beginLineIndentation + this.Options.IndentString, nodes);
		}
		/// <summary>
		/// Ensure that code is inserted correctly in {} code blocks - SD2-1180
		/// </summary>
		public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
		{
			string beginLineIndentation = GetIndentation(document, region.BeginLine);
			int insertionLine = region.EndLine - 1;
			
			IRefactoringDocumentLine endLine = document.GetLine(region.EndLine);
			string endLineText = endLine.Text;
			int originalPos = region.EndColumn - 2; // -1 for column coordinate => offset, -1 because EndColumn is after the '}'
			int pos = originalPos;
			if (pos < 0 || pos >= endLineText.Length || endLineText[pos] != '}') {
				LoggingService.Warn("CSharpCodeGenerator.InsertCodeAtEnd: position is invalid (not pointing to '}')"
				                    + " endLineText=" + endLineText + ", pos=" + pos);
			} else {
				for (pos--; pos >= 0; pos--) {
					if (!char.IsWhiteSpace(endLineText[pos])) {
						// range before '}' is not empty: we cannot simply insert in the line before the '}', so
						// 
						pos++; // set pos to first whitespace character / the '{' character
						if (pos < originalPos) {
							// remove whitespace between last non-white character and the '}'
							document.Remove(endLine.Offset + pos, originalPos - pos);
						}
						// insert newline and same indentation as used in beginLine before the '}'
						document.Insert(endLine.Offset + pos, Environment.NewLine + beginLineIndentation);
						insertionLine++;
						
						pos = region.BeginColumn - 1;
						if (region.BeginLine == region.EndLine && pos >= 1 && pos < endLineText.Length) {
							// The whole block was in on a single line, e.g. "get { return field; }".
							// Insert an additional newline after the '{'.
							
							originalPos = pos = endLineText.IndexOf('{', pos);
							if (pos >= 0 && pos < region.EndColumn - 1) {
								// find next non-whitespace after originalPos
								originalPos++; // point to insertion position for newline after {
								for (pos++; pos < endLineText.Length; pos++) {
									if (!char.IsWhiteSpace(endLineText[pos])) {
										// remove all between originalPos and pos
										if (originalPos < pos) {
											document.Remove(endLine.Offset + originalPos, pos - originalPos);
										}
										document.Insert(endLine.Offset + originalPos, Environment.NewLine + beginLineIndentation + '\t');
										insertionLine++;
										break;
									}
								}
							}
						}
						break;
					}
				}
			}
			InsertCodeAfter(insertionLine, document, beginLineIndentation + this.Options.IndentString, nodes);
		}
Exemple #10
0
        public static void ImplementAbstractClass(IRefactoringDocument doc, IClass target, IReturnType abstractClass)
        {
            CodeGenerator generator = target.ProjectContent.Language.CodeGenerator;
            var           pos       = doc.OffsetToPosition(doc.PositionToOffset(target.BodyRegion.EndLine, target.BodyRegion.EndColumn) - 1);
            ClassFinder   context   = new ClassFinder(target, pos.Line, pos.Column);

            foreach (IMember member in MemberLookupHelper.GetAccessibleMembers(abstractClass, target, LanguageProperties.CSharp, true)
                     .Where(m => m.IsAbstract && !target.HasMember(m)))
            {
                generator.InsertCodeAtEnd(target.BodyRegion, doc, generator.GetOverridingMethod(member, context));
            }
        }
 public virtual void InsertCodeAfter(IMember member, IRefactoringDocument document, params AbstractNode[] nodes)
 {
     if (member is IMethodOrProperty)
     {
         InsertCodeAfter(((IMethodOrProperty)member).BodyRegion.EndLine, document,
                         GetIndentation(document, member.Region.BeginLine), nodes);
     }
     else
     {
         InsertCodeAfter(member.Region.EndLine, document,
                         GetIndentation(document, member.Region.BeginLine), nodes);
     }
 }
Exemple #12
0
 public virtual void InsertCodeAfter(IMember member, IRefactoringDocument document, params AbstractNode[] nodes)
 {
     if (member is IMethodOrProperty)
     {
         InsertCodeAfter(((IMethodOrProperty)member).BodyRegion.EndLine, document,
                         GetIndentation(document, member.Region.BeginLine), nodes);
     }
     else
     {
         int line = member.Region.EndLine;
         // VB uses the position after the EOL as end location for fields, so insert after
         // the previous line if the end position is pointing to the start of a line.
         if (member.Region.EndColumn == 1)
         {
             line--;
         }
         InsertCodeAfter(line, document,
                         GetIndentation(document, member.Region.BeginLine), nodes);
     }
 }
		public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
		{
			RegionPassedToInsertCodeAtEnd = region;
			DocumentPassedToInsertCodeAtEnd = document;
			NodePassedToInsertCodeAtEnd = nodes.FirstOrDefault();
		}
Exemple #14
0
 public void Init()
 {
     methodHelper   = new MethodHelper();
     document       = MockRepository.GenerateStub <IRefactoringDocument>();
     documentLoader = MockRepository.GenerateStub <IDocumentLoader>();
 }
 void OpenDocument()
 {
     documentView = DocumentLoader.LoadRefactoringDocumentView(FilePosition.FileName);
     document     = documentView.RefactoringDocument;
 }
		public void Init()
		{
			classHelper = new ClassHelper();
			document = MockRepository.GenerateStub<IRefactoringDocument>();
			documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
		}
Exemple #17
0
        protected string GetIndentation(IRefactoringDocument document, int line)
        {
            string lineText = document.GetLine(line).Text;

            return(lineText.Substring(0, lineText.Length - lineText.TrimStart().Length));
        }
Exemple #18
0
 public virtual void InsertCodeInClass(IClass c, IRefactoringDocument document, int targetLine, params AbstractNode[] nodes)
 {
     InsertCodeAfter(targetLine, document,
                     GetIndentation(document, c.Region.BeginLine) + options.IndentString, false, nodes);
 }
Exemple #19
0
 public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
 {
     InsertCodeAfter(region.EndLine, document,
                     GetIndentation(document, region.BeginLine) + '\t', nodes);
 }
 void OpenFileContainingClass()
 {
     Document = DocumentLoader.LoadRefactoringDocument(Class.CompilationUnit.FileName);
 }
Exemple #21
0
 public virtual void InsertCodeAfter(IClass @class, IRefactoringDocument document, params AbstractNode[] nodes)
 {
     InsertCodeAfter(@class.BodyRegion.EndLine, document,
                     GetIndentation(document, @class.BodyRegion.BeginLine), nodes);
 }
Exemple #22
0
        public virtual void ReplaceUsings(IRefactoringDocument document, IList <IUsing> oldUsings, IList <IUsing> newUsings)
        {
            if (oldUsings.Count == newUsings.Count)
            {
                bool identical = true;
                for (int i = 0; i < oldUsings.Count; i++)
                {
                    if (oldUsings[i] != newUsings[i])
                    {
                        identical = false;
                        break;
                    }
                }
                if (identical)
                {
                    return;
                }
            }

            int firstLine = int.MaxValue;
            List <KeyValuePair <int, int> > regions = new List <KeyValuePair <int, int> >();

            foreach (IUsing u in oldUsings)
            {
                if (u.Region.BeginLine < firstLine)
                {
                    firstLine = u.Region.BeginLine;
                }
                int st = document.PositionToOffset(u.Region.BeginLine, u.Region.BeginColumn);
                int en = document.PositionToOffset(u.Region.EndLine, u.Region.EndColumn);
                regions.Add(new KeyValuePair <int, int>(st, en - st));
            }

            regions.Sort(delegate(KeyValuePair <int, int> a, KeyValuePair <int, int> b) {
                return(a.Key.CompareTo(b.Key));
            });
            int    insertionOffset = regions.Count == 0 ? 0 : regions[0].Key;
            string indentation;

            if (firstLine != int.MaxValue)
            {
                indentation      = GetIndentation(document, firstLine);
                insertionOffset -= indentation.Length;
            }
            else
            {
                indentation = "";
            }

            document.StartUndoableAction();
            for (int i = regions.Count - 1; i >= 0; i--)
            {
                document.Remove(regions[i].Key, regions[i].Value);
            }
            int lastNewLine = insertionOffset;

            for (int i = insertionOffset; i < document.TextLength; i++)
            {
                char c = document.GetCharAt(i);
                if (!char.IsWhiteSpace(c))
                {
                    break;
                }
                if (c == '\n')
                {
                    if (i > 0 && document.GetCharAt(i - 1) == '\r')
                    {
                        lastNewLine = i - 1;
                    }
                    else
                    {
                        lastNewLine = i;
                    }
                }
            }
            if (lastNewLine != insertionOffset)
            {
                document.Remove(insertionOffset, lastNewLine - insertionOffset);
            }
            StringBuilder txt = new StringBuilder();

            foreach (IUsing us in newUsings)
            {
                if (us == null)
                {
                    txt.AppendLine(indentation);
                }
                else
                {
                    txt.Append(GenerateCode(ConvertUsing(us), indentation));
                }
            }
            document.Insert(insertionOffset, txt.ToString());
            document.EndUndoableAction();
        }
 void OpenFileContainingMethod()
 {
     Document = DocumentLoader.LoadRefactoringDocument(Method.CompilationUnit.FileName);
 }
		public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
		{
			InsertCodeAfter(region.EndLine, document,
			                GetIndentation(document, region.BeginLine) + '\t', nodes);
		}
Exemple #25
0
 /// <summary>
 /// Generates code for <paramref name="nodes"/> and inserts it into <paramref name="document"/>
 /// after the line <paramref name="insertLine"/>.
 /// </summary>
 protected void InsertCodeAfter(int insertLine, IRefactoringDocument document, string indentation, params AbstractNode[] nodes)
 {
     InsertCodeAfter(insertLine, document, indentation, true, nodes);
 }
Exemple #26
0
		void OpenDocument()
		{
			documentView = DocumentLoader.LoadRefactoringDocumentView(FilePosition.FileName);
			document = documentView.RefactoringDocument;
		}
Exemple #27
0
 public virtual void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
 {
     InsertCodeAfter(region.EndLine - 1, document,
                     GetIndentation(document, region.BeginLine) + options.IndentString, nodes);
 }
 public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
 {
     RegionPassedToInsertCodeAtEnd   = region;
     DocumentPassedToInsertCodeAtEnd = document;
     NodePassedToInsertCodeAtEnd     = nodes.FirstOrDefault();
 }