7 changed files with 139 additions and 4 deletions
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using CSharpBinding.FormattingStrategy; |
||||
using ICSharpCode.NRefactory.CSharp; |
||||
using ICSharpCode.NRefactory.CSharp.Refactoring; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
using ICSharpCode.NRefactory.CSharp.TypeSystem; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
using ICSharpCode.SharpDevelop; |
||||
using ICSharpCode.SharpDevelop.Editor.CodeCompletion; |
||||
|
||||
namespace CSharpBinding.Completion |
||||
{ |
||||
/// <summary>
|
||||
/// Item for 'override' completion.
|
||||
/// </summary>
|
||||
class OverrideCompletionData : EntityCompletionData |
||||
{ |
||||
readonly int declarationBegin; |
||||
readonly CSharpTypeResolveContext contextAtCaret; |
||||
|
||||
public OverrideCompletionData(int declarationBegin, IMember m, CSharpTypeResolveContext contextAtCaret) |
||||
: base(m) |
||||
{ |
||||
this.declarationBegin = declarationBegin; |
||||
this.contextAtCaret = contextAtCaret; |
||||
var ambience = new CSharpAmbience(); |
||||
ambience.ConversionFlags = ConversionFlags.ShowTypeParameterList | ConversionFlags.ShowParameterList | ConversionFlags.ShowParameterNames; |
||||
this.CompletionText = ambience.ConvertEntity(m); |
||||
} |
||||
|
||||
public override void Complete(CompletionContext context) |
||||
{ |
||||
if (declarationBegin > context.StartOffset) { |
||||
base.Complete(context); |
||||
return; |
||||
} |
||||
TypeSystemAstBuilder b = new TypeSystemAstBuilder(new CSharpResolver(contextAtCaret)); |
||||
b.ShowTypeParameterConstraints = false; |
||||
b.GenerateBody = true; |
||||
|
||||
var entityDeclaration = b.ConvertEntity(this.Entity); |
||||
entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract); |
||||
entityDeclaration.Modifiers |= Modifiers.Override; |
||||
|
||||
StringWriter w = new StringWriter(); |
||||
var segmentDict = SegmentTrackingOutputFormatter.WriteNode(w, entityDeclaration, FormattingOptionsFactory.CreateSharpDevelop()); |
||||
|
||||
context.Editor.Document.Replace(declarationBegin, context.EndOffset - declarationBegin, w.ToString().TrimEnd()); |
||||
var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement); |
||||
if (throwStatement != null) { |
||||
var segment = segmentDict[throwStatement]; |
||||
context.Editor.Select(declarationBegin + segment.Offset, segment.Length); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Text; |
||||
using ICSharpCode.AvalonEdit.Document; |
||||
using ICSharpCode.NRefactory.CSharp; |
||||
using ICSharpCode.NRefactory.Editor; |
||||
|
||||
namespace CSharpBinding.Completion |
||||
{ |
||||
/// <summary>
|
||||
/// Output formatter that creates a dictionary from AST nodes to segments in the output text.
|
||||
/// </summary>
|
||||
public class SegmentTrackingOutputFormatter : TextWriterOutputFormatter |
||||
{ |
||||
Dictionary<AstNode, ISegment> segments = new Dictionary<AstNode, ISegment>(); |
||||
Stack<int> startOffsets = new Stack<int>(); |
||||
readonly StringWriter stringWriter; |
||||
|
||||
public IReadOnlyDictionary<AstNode, ISegment> Segments { |
||||
get { return segments; } |
||||
} |
||||
|
||||
public SegmentTrackingOutputFormatter (StringWriter stringWriter) |
||||
: base(stringWriter) |
||||
{ |
||||
this.stringWriter = stringWriter; |
||||
} |
||||
|
||||
public static IReadOnlyDictionary<AstNode, ISegment> WriteNode(StringWriter writer, AstNode node, CSharpFormattingOptions policy) |
||||
{ |
||||
var formatter = new SegmentTrackingOutputFormatter(writer); |
||||
var visitor = new CSharpOutputVisitor(formatter, policy); |
||||
node.AcceptVisitor(visitor); |
||||
return formatter.Segments; |
||||
} |
||||
|
||||
public override void StartNode (AstNode node) |
||||
{ |
||||
base.StartNode (node); |
||||
startOffsets.Push(stringWriter.GetStringBuilder ().Length); |
||||
} |
||||
|
||||
public override void EndNode (AstNode node) |
||||
{ |
||||
int startOffset = startOffsets.Pop(); |
||||
StringBuilder b = stringWriter.GetStringBuilder(); |
||||
int endOffset = b.Length; |
||||
while (endOffset > 0 && b[endOffset - 1] == '\r' || b[endOffset - 1] == '\n') |
||||
endOffset--; |
||||
segments.Add(node, new TextSegment { StartOffset = startOffset, EndOffset = endOffset }); |
||||
base.EndNode (node); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue