Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

put new type under namespace #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 79 additions & 61 deletions OmniSharp/Refactoring/OmniSharpScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace OmniSharp.Refactoring
public class OmniSharpScript : DocumentScript
{
readonly OmniSharpRefactoringContext _context;

public OmniSharpScript(OmniSharpRefactoringContext context, OmniSharpConfiguration config)
: base(context.Document, config.CSharpFormattingOptions, config.TextEditorOptions)
{
Expand All @@ -27,44 +28,52 @@ public override Task Link(params AstNode[] nodes)
{
Debug.Assert(GetSegment(node) != null);
}
return new Task(() => { });
return new Task(() =>
{
});
}

public override Task<Script> InsertWithCursor(string operation, InsertPosition defaultPosition, IList<AstNode> nodes)
{
EntityDeclaration entity = _context.GetNode<EntityDeclaration>();
if (entity is Accessor) {
entity = (EntityDeclaration) entity.Parent;
}

foreach (var node in nodes) {
InsertBefore(entity, node);
}
var tcs = new TaskCompletionSource<Script> ();
tcs.SetResult (this);
return tcs.Task;
}

public override Task<Script> InsertWithCursor(string operation, ITypeDefinition parentType, Func<Script, RefactoringContext, IList<AstNode>> nodeCallback)
{
var unit = _context.RootNode;
var insertType = unit.GetNodeAt<TypeDeclaration> (parentType.Region.Begin);

var startOffset = GetCurrentOffset (insertType.LBraceToken.EndLocation);
var nodes = nodeCallback(this, _context);
foreach (var node in nodes.Reverse ()) {
var output = OutputNode (1, node, true);
if (parentType.Kind == TypeKind.Enum) {
InsertText (startOffset, output.Text + (!parentType.Fields.Any() ? "" : ","));
} else {
InsertText (startOffset, output.Text);
}
output.RegisterTrackedSegments (this, startOffset);
}
var tcs = new TaskCompletionSource<Script> ();
tcs.SetResult (this);
return tcs.Task;
}
public override Task<Script> InsertWithCursor(string operation, InsertPosition defaultPosition, IList<AstNode> nodes)
{
EntityDeclaration entity = _context.GetNode<EntityDeclaration>();
if (entity is Accessor)
{
entity = (EntityDeclaration)entity.Parent;
}

foreach (var node in nodes)
{
InsertBefore(entity, node);
}
var tcs = new TaskCompletionSource<Script>();
tcs.SetResult(this);
return tcs.Task;
}

public override Task<Script> InsertWithCursor(string operation, ITypeDefinition parentType, Func<Script, RefactoringContext, IList<AstNode>> nodeCallback)
{
var unit = _context.RootNode;
var insertType = unit.GetNodeAt<TypeDeclaration>(parentType.Region.Begin);

var startOffset = GetCurrentOffset(insertType.LBraceToken.EndLocation);
var nodes = nodeCallback(this, _context);
foreach (var node in nodes.Reverse ())
{
var output = OutputNode(1, node, true);
if (parentType.Kind == TypeKind.Enum)
{
InsertText(startOffset, output.Text + (!parentType.Fields.Any() ? "" : ","));
}
else
{
InsertText(startOffset, output.Text);
}
output.RegisterTrackedSegments(this, startOffset);
}
var tcs = new TaskCompletionSource<Script>();
tcs.SetResult(this);
return tcs.Task;
}

public void Rename(AstNode node, string newName)
{
Expand Down Expand Up @@ -94,40 +103,49 @@ public void Rename(AstNode node, string newName)
Replace(node, new IdentifierExpression(newName));
}

public override void Rename(ISymbol symbol, string name = null)
public override void Rename(ISymbol symbol, string name = null)
{
FindReferences refFinder = new FindReferences();
refFinder.FindReferencesInFile(refFinder.GetSearchScopes(symbol),
_context.UnresolvedFile,
_context.RootNode as SyntaxTree,
_context.Compilation, (n, r) => Rename(n, name),
_context.CancellationToken);
_context.UnresolvedFile,
_context.RootNode as SyntaxTree,
_context.Compilation, (n, r) => Rename(n, name),
_context.CancellationToken);
}

// public override void Rename(IVariable variable, string name)
// {
// FindReferences refFinder = new FindReferences();
// refFinder.FindLocalReferences(variable,
// _context.UnresolvedFile,
// _context.RootNode as SyntaxTree,
// _context.Compilation, (n, r) => Rename(n, name),
// _context.CancellationToken);
// }
//
// public override void RenameTypeParameter(IType type, string name = null)
// {
// FindReferences refFinder = new FindReferences();
// refFinder.FindTypeParameterReferences(type,
// _context.UnresolvedFile,
// _context.RootNode as SyntaxTree,
// _context.Compilation, (n, r) => Rename(n, name),
// _context.CancellationToken);
// }
// public override void Rename(IVariable variable, string name)
// {
// FindReferences refFinder = new FindReferences();
// refFinder.FindLocalReferences(variable,
// _context.UnresolvedFile,
// _context.RootNode as SyntaxTree,
// _context.Compilation, (n, r) => Rename(n, name),
// _context.CancellationToken);
// }
//
// public override void RenameTypeParameter(IType type, string name = null)
// {
// FindReferences refFinder = new FindReferences();
// refFinder.FindTypeParameterReferences(type,
// _context.UnresolvedFile,
// _context.RootNode as SyntaxTree,
// _context.Compilation, (n, r) => Rename(n, name),
// _context.CancellationToken);
// }

public override void CreateNewType(AstNode newType, NewTypeContext context = NewTypeContext.CurrentNamespace)
{
var output = OutputNode(0, newType, true);
InsertText(0, output.Text);
var firstCurlyBraceIndex = this.CurrentDocument.Text.IndexOf("{");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code isn't quite right.... what if there is no namespace? You probably want to search for the first class instead and insert before that. You should use the AST for that.

Also, please include some tests so I can see what is going on. A test with a class with a namespace, class without namespace, namespace without a class etc.

if (firstCurlyBraceIndex < 0)
{
firstCurlyBraceIndex = 0;
}
else
{
firstCurlyBraceIndex = firstCurlyBraceIndex + 1;
}
InsertText(firstCurlyBraceIndex, output.Text);
}
}
}