11 changed files with 288 additions and 148 deletions
@ -0,0 +1,158 @@ |
|||||||
|
// 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.Threading; |
||||||
|
using CSharpBinding.Parser; |
||||||
|
using ICSharpCode.NRefactory; |
||||||
|
using ICSharpCode.NRefactory.CSharp; |
||||||
|
using ICSharpCode.NRefactory.CSharp.Refactoring; |
||||||
|
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||||
|
using ICSharpCode.NRefactory.Editor; |
||||||
|
using ICSharpCode.NRefactory.Semantics; |
||||||
|
using ICSharpCode.NRefactory.TypeSystem; |
||||||
|
using ICSharpCode.SharpDevelop; |
||||||
|
using ICSharpCode.SharpDevelop.Editor; |
||||||
|
using ICSharpCode.SharpDevelop.Parser; |
||||||
|
using ICSharpCode.SharpDevelop.Refactoring; |
||||||
|
|
||||||
|
namespace CSharpBinding.Refactoring |
||||||
|
{ |
||||||
|
public class SDRefactoringContext : RefactoringContext |
||||||
|
{ |
||||||
|
readonly ITextEditor editor; |
||||||
|
readonly ITextSource textSource; |
||||||
|
volatile IDocument document; |
||||||
|
readonly CSharpAstResolver resolver; |
||||||
|
int selectionStart, selectionLength; |
||||||
|
CancellationToken cancellationToken; |
||||||
|
|
||||||
|
public SDRefactoringContext(ITextSource textSource, CSharpAstResolver resolver, TextLocation location, int selectionStart, int selectionLength, CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
this.textSource = textSource; |
||||||
|
this.document = textSource as IDocument; |
||||||
|
this.resolver = resolver; |
||||||
|
this.selectionStart = selectionStart; |
||||||
|
this.selectionLength = selectionLength; |
||||||
|
this.cancellationToken = cancellationToken; |
||||||
|
|
||||||
|
this.Unit = resolver.RootNode as CompilationUnit; |
||||||
|
this.Compilation = resolver.Compilation; |
||||||
|
this.Location = location; |
||||||
|
} |
||||||
|
|
||||||
|
public SDRefactoringContext(ITextEditor editor, CSharpAstResolver resolver, TextLocation location) |
||||||
|
{ |
||||||
|
this.editor = editor; |
||||||
|
this.textSource = editor.Document; |
||||||
|
this.document = editor.Document; |
||||||
|
this.resolver = resolver; |
||||||
|
this.selectionStart = editor.SelectionStart; |
||||||
|
this.selectionLength = editor.SelectionLength; |
||||||
|
|
||||||
|
this.Unit = resolver.RootNode as CompilationUnit; |
||||||
|
this.Compilation = resolver.Compilation; |
||||||
|
this.Location = location; |
||||||
|
} |
||||||
|
|
||||||
|
public override bool Supports(Version version) |
||||||
|
{ |
||||||
|
CSharpProject project = resolver.TypeResolveContext.Compilation.GetProject() as CSharpProject; |
||||||
|
if (project == null) |
||||||
|
return false; |
||||||
|
return project.LanguageVersion >= version; |
||||||
|
} |
||||||
|
|
||||||
|
public override NodeOutputAction CreateNodeOutputAction(int offset, int removedChars, NodeOutput output) |
||||||
|
{ |
||||||
|
return new SDScript.SDNodeOutputAction(document, offset, removedChars, output); |
||||||
|
} |
||||||
|
|
||||||
|
public override Script StartScript() |
||||||
|
{ |
||||||
|
if (editor == null) |
||||||
|
throw new InvalidOperationException("Cannot start a script in IsAvailable()."); |
||||||
|
return new SDScript(editor, this); |
||||||
|
} |
||||||
|
|
||||||
|
public override int SelectionStart { |
||||||
|
get { return selectionStart; } |
||||||
|
} |
||||||
|
|
||||||
|
public override int SelectionLength { |
||||||
|
get { return selectionLength; } |
||||||
|
} |
||||||
|
|
||||||
|
public override int SelectionEnd { |
||||||
|
get { |
||||||
|
return selectionStart + selectionLength; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override string SelectedText { |
||||||
|
get { |
||||||
|
return textSource.GetText(selectionStart, selectionLength); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override ResolveResult Resolve(AstNode expression) |
||||||
|
{ |
||||||
|
lock (resolver) |
||||||
|
return resolver.Resolve(expression, cancellationToken); |
||||||
|
} |
||||||
|
|
||||||
|
public override void ReplaceReferences(IMember member, MemberDeclaration replaceWidth) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public override bool IsSomethingSelected { |
||||||
|
get { |
||||||
|
return selectionLength > 0; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override string GetText(int offset, int length) |
||||||
|
{ |
||||||
|
return textSource.GetText(offset, length); |
||||||
|
} |
||||||
|
|
||||||
|
public override int GetOffset(TextLocation location) |
||||||
|
{ |
||||||
|
if (document == null) |
||||||
|
document = new ReadOnlyDocument(textSource); |
||||||
|
return document.GetOffset(location); |
||||||
|
} |
||||||
|
|
||||||
|
public override TextLocation GetLocation(int offset) |
||||||
|
{ |
||||||
|
if (document == null) |
||||||
|
document = new ReadOnlyDocument(textSource); |
||||||
|
return document.GetLocation(offset); |
||||||
|
} |
||||||
|
|
||||||
|
public override CSharpFormattingOptions FormattingOptions { |
||||||
|
get { |
||||||
|
return new CSharpFormattingOptions(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override string EolMarker { |
||||||
|
get { |
||||||
|
if (document == null) |
||||||
|
document = new ReadOnlyDocument(textSource); |
||||||
|
return DocumentUtilitites.GetLineTerminator(document, 1); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override AstType CreateShortType(IType fullType) |
||||||
|
{ |
||||||
|
CSharpResolver csResolver; |
||||||
|
lock (resolver) { |
||||||
|
csResolver = resolver.GetResolverStateBefore(GetNode()); |
||||||
|
} |
||||||
|
var builder = new TypeSystemAstBuilder(csResolver); |
||||||
|
return builder.ConvertType(fullType); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,83 @@ |
|||||||
|
// 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 ICSharpCode.NRefactory.CSharp; |
||||||
|
using ICSharpCode.NRefactory.CSharp.Refactoring; |
||||||
|
using ICSharpCode.NRefactory.Editor; |
||||||
|
using ICSharpCode.SharpDevelop.Editor; |
||||||
|
|
||||||
|
namespace CSharpBinding.Refactoring |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Refactoring change script.
|
||||||
|
/// </summary>
|
||||||
|
sealed class SDScript : Script |
||||||
|
{ |
||||||
|
readonly ITextEditor editor; |
||||||
|
|
||||||
|
public SDScript(ITextEditor editor, SDRefactoringContext context) : base(context) |
||||||
|
{ |
||||||
|
this.editor = editor; |
||||||
|
} |
||||||
|
|
||||||
|
public static void RunActions (IList<ICSharpCode.NRefactory.CSharp.Refactoring.Action> actions, Script script) |
||||||
|
{ |
||||||
|
for (int i = 0; i < actions.Count; i++) { |
||||||
|
actions [i].Perform (script); |
||||||
|
var replaceChange = actions [i] as TextReplaceAction; |
||||||
|
if (replaceChange == null) |
||||||
|
continue; |
||||||
|
for (int j = 0; j < actions.Count; j++) { |
||||||
|
if (i == j) |
||||||
|
continue; |
||||||
|
var change = actions [j] as TextReplaceAction; |
||||||
|
if (change == null) |
||||||
|
continue; |
||||||
|
if (replaceChange.Offset >= 0 && change.Offset >= 0) { |
||||||
|
if (replaceChange.Offset < change.Offset) { |
||||||
|
change.Offset -= replaceChange.RemovedChars; |
||||||
|
if (!string.IsNullOrEmpty (replaceChange.InsertedText)) |
||||||
|
change.Offset += replaceChange.InsertedText.Length; |
||||||
|
} else if (replaceChange.Offset < change.Offset + change.RemovedChars) { |
||||||
|
change.RemovedChars = Math.Max (0, change.RemovedChars - replaceChange.RemovedChars); |
||||||
|
change.Offset = replaceChange.Offset + (!string.IsNullOrEmpty (replaceChange.InsertedText) ? replaceChange.InsertedText.Length : 0); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override void Dispose () |
||||||
|
{ |
||||||
|
using (editor.Document.OpenUndoGroup ()) { |
||||||
|
RunActions (changes, this); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override void InsertWithCursor(string operation, AstNode node, InsertPosition defaultPosition) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
internal class SDNodeOutputAction : NodeOutputAction |
||||||
|
{ |
||||||
|
IDocument doc; |
||||||
|
|
||||||
|
public SDNodeOutputAction(IDocument doc, int offset, int removedChars, NodeOutput output) : base (offset, removedChars, output) |
||||||
|
{ |
||||||
|
if (doc == null) |
||||||
|
throw new ArgumentNullException ("doc"); |
||||||
|
if (output == null) |
||||||
|
throw new ArgumentNullException ("output"); |
||||||
|
this.doc = doc; |
||||||
|
} |
||||||
|
|
||||||
|
public override void Perform (Script script) |
||||||
|
{ |
||||||
|
doc.Replace (Offset, RemovedChars, NodeOutput.Text); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -1,123 +0,0 @@ |
|||||||
// 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.Threading; |
|
||||||
using CSharpBinding.Parser; |
|
||||||
using ICSharpCode.NRefactory; |
|
||||||
using ICSharpCode.NRefactory.CSharp; |
|
||||||
using ICSharpCode.NRefactory.CSharp.Refactoring; |
|
||||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
|
||||||
using ICSharpCode.NRefactory.Semantics; |
|
||||||
using ICSharpCode.NRefactory.TypeSystem; |
|
||||||
using ICSharpCode.SharpDevelop; |
|
||||||
using ICSharpCode.SharpDevelop.Editor; |
|
||||||
using ICSharpCode.SharpDevelop.Parser; |
|
||||||
|
|
||||||
namespace CSharpBinding |
|
||||||
{ |
|
||||||
public class SDRefactoringContext : RefactoringContext |
|
||||||
{ |
|
||||||
ITextEditor editor; |
|
||||||
ICompilation compilation; |
|
||||||
CancellationToken cancellationToken; |
|
||||||
|
|
||||||
public SDRefactoringContext(ITextEditor editor, CancellationToken cancellationToken) |
|
||||||
{ |
|
||||||
this.editor = editor; |
|
||||||
this.cancellationToken = cancellationToken; |
|
||||||
this.compilation = ParserService.GetCompilationForFile(editor.FileName); |
|
||||||
} |
|
||||||
|
|
||||||
public override bool Supports(Version version) |
|
||||||
{ |
|
||||||
CSharpProject project = compilation.GetProject() as CSharpProject; |
|
||||||
if (project == null) |
|
||||||
return false; |
|
||||||
return project.LanguageVersion >= version; |
|
||||||
} |
|
||||||
|
|
||||||
public override Script StartScript() |
|
||||||
{ |
|
||||||
throw new NotImplementedException(); |
|
||||||
} |
|
||||||
|
|
||||||
public override int SelectionStart { |
|
||||||
get { |
|
||||||
return editor.SelectionStart; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public override int SelectionLength { |
|
||||||
get { |
|
||||||
return editor.SelectionLength; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public override int SelectionEnd { |
|
||||||
get { |
|
||||||
return editor.SelectionStart + editor.SelectionLength; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public override string SelectedText { |
|
||||||
get { |
|
||||||
return editor.SelectedText; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public override ResolveResult Resolve(AstNode expression) |
|
||||||
{ |
|
||||||
var parseInfo = ParserService.Parse(editor.FileName, editor.Document) as CSharpFullParseInformation; |
|
||||||
var resolver = new CSharpAstResolver(compilation, parseInfo.CompilationUnit, parseInfo.ParsedFile); |
|
||||||
return resolver.Resolve(expression, cancellationToken); |
|
||||||
} |
|
||||||
|
|
||||||
public override void ReplaceReferences(IMember member, MemberDeclaration replaceWidth) |
|
||||||
{ |
|
||||||
throw new NotImplementedException(); |
|
||||||
} |
|
||||||
|
|
||||||
public override bool IsSomethingSelected { |
|
||||||
get { |
|
||||||
return editor.SelectionLength > 0; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public override string GetText(int offset, int length) |
|
||||||
{ |
|
||||||
return editor.Document.GetText(offset, length); |
|
||||||
} |
|
||||||
|
|
||||||
public override int GetOffset(TextLocation location) |
|
||||||
{ |
|
||||||
return editor.Document.GetOffset(location); |
|
||||||
} |
|
||||||
|
|
||||||
public override TextLocation GetLocation(int offset) |
|
||||||
{ |
|
||||||
return editor.Document.GetLocation(offset); |
|
||||||
} |
|
||||||
|
|
||||||
public override CSharpFormattingOptions FormattingOptions { |
|
||||||
get { |
|
||||||
return new CSharpFormattingOptions(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public override string EolMarker { |
|
||||||
get { |
|
||||||
return DocumentUtilitites.GetLineTerminator(editor.Document, 1); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public override AstType CreateShortType(IType fullType) |
|
||||||
{ |
|
||||||
var parseInfo = ParserService.Parse(editor.FileName, editor.Document) as CSharpFullParseInformation; |
|
||||||
var parsedFile = parseInfo.ParsedFile; |
|
||||||
var csResolver = parsedFile.GetResolver(compilation, editor.Caret.Location); |
|
||||||
var builder = new TypeSystemAstBuilder(csResolver); |
|
||||||
return builder.ConvertType(fullType); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue