コード例 #1
0
 void CreateDocumentLoader()
 {
     document     = MockRepository.GenerateStub <IRefactoringDocument>();
     documentView = MockRepository.GenerateStub <IRefactoringDocumentView>();
     documentView.Stub(view => view.RefactoringDocument).Return(document);
     documentLoader = MockRepository.GenerateStub <IDocumentLoader>();
 }
コード例 #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());
            }
        }
コード例 #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));
        }
コード例 #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());
        }
コード例 #5
0
		void CreateDocumentLoader()
		{
			document = MockRepository.GenerateStub<IRefactoringDocument>();
			documentView = MockRepository.GenerateStub<IRefactoringDocumentView>();
			documentView.Stub(view => view.RefactoringDocument).Return(document);
			documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
		}
コード例 #6
0
		public void Init()
		{
			helper = new ClassHelper();
			document = MockRepository.GenerateStub<IRefactoringDocument>();
			documentView = MockRepository.GenerateStub<IRefactoringDocumentView>();
			documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
			fakeCodeGenerator = helper.CompilationUnitHelper.FakeCodeGenerator;
		}
コード例 #7
0
 public void Init()
 {
     helper            = new ClassHelper();
     document          = MockRepository.GenerateStub <IRefactoringDocument>();
     documentView      = MockRepository.GenerateStub <IRefactoringDocumentView>();
     documentLoader    = MockRepository.GenerateStub <IDocumentLoader>();
     fakeCodeGenerator = helper.CompilationUnitHelper.FakeCodeGenerator;
 }
コード例 #8
0
ファイル: CSharpCodeGenerator.cs プロジェクト: giszzt/GeoSOS
		/// <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);
		}
コード例 #9
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);
		}
コード例 #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));
            }
        }
コード例 #11
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
     {
         InsertCodeAfter(member.Region.EndLine, document,
                         GetIndentation(document, member.Region.BeginLine), nodes);
     }
 }
コード例 #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);
     }
 }
コード例 #13
0
		public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
		{
			RegionPassedToInsertCodeAtEnd = region;
			DocumentPassedToInsertCodeAtEnd = document;
			NodePassedToInsertCodeAtEnd = nodes.FirstOrDefault();
		}
コード例 #14
0
 public void Init()
 {
     methodHelper   = new MethodHelper();
     document       = MockRepository.GenerateStub <IRefactoringDocument>();
     documentLoader = MockRepository.GenerateStub <IDocumentLoader>();
 }
コード例 #15
0
 void OpenDocument()
 {
     documentView = DocumentLoader.LoadRefactoringDocumentView(FilePosition.FileName);
     document     = documentView.RefactoringDocument;
 }
コード例 #16
0
		public void Init()
		{
			classHelper = new ClassHelper();
			document = MockRepository.GenerateStub<IRefactoringDocument>();
			documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
		}
コード例 #17
0
        protected string GetIndentation(IRefactoringDocument document, int line)
        {
            string lineText = document.GetLine(line).Text;

            return(lineText.Substring(0, lineText.Length - lineText.TrimStart().Length));
        }
コード例 #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);
 }
コード例 #19
0
 public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
 {
     InsertCodeAfter(region.EndLine, document,
                     GetIndentation(document, region.BeginLine) + '\t', nodes);
 }
コード例 #20
0
 void OpenFileContainingClass()
 {
     Document = DocumentLoader.LoadRefactoringDocument(Class.CompilationUnit.FileName);
 }
コード例 #21
0
 public virtual void InsertCodeAfter(IClass @class, IRefactoringDocument document, params AbstractNode[] nodes)
 {
     InsertCodeAfter(@class.BodyRegion.EndLine, document,
                     GetIndentation(document, @class.BodyRegion.BeginLine), nodes);
 }
コード例 #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();
        }
コード例 #23
0
 void OpenFileContainingMethod()
 {
     Document = DocumentLoader.LoadRefactoringDocument(Method.CompilationUnit.FileName);
 }
コード例 #24
0
		public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
		{
			InsertCodeAfter(region.EndLine, document,
			                GetIndentation(document, region.BeginLine) + '\t', nodes);
		}
コード例 #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);
 }
コード例 #26
0
ファイル: EditPoint.cs プロジェクト: Netring/SharpDevelop
		void OpenDocument()
		{
			documentView = DocumentLoader.LoadRefactoringDocumentView(FilePosition.FileName);
			document = documentView.RefactoringDocument;
		}
コード例 #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);
 }
コード例 #28
0
 public override void InsertCodeAtEnd(DomRegion region, IRefactoringDocument document, params AbstractNode[] nodes)
 {
     RegionPassedToInsertCodeAtEnd   = region;
     DocumentPassedToInsertCodeAtEnd = document;
     NodePassedToInsertCodeAtEnd     = nodes.FirstOrDefault();
 }