Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6179 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61pull/1/head
13 changed files with 235 additions and 17 deletions
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Martin Konicek" email="martin.konicek@gmail.com"/>
|
||||
// <version>$Revision: $</version>
|
||||
// </file>
|
||||
using System; |
||||
using System.Collections.ObjectModel; |
||||
using ICSharpCode.NRefactory.Ast; |
||||
|
||||
namespace SharpRefactoring.ContextActions |
||||
{ |
||||
public static class Extensions |
||||
{ |
||||
/// <summary>
|
||||
/// Finds TypeDeclarations in AST tree.
|
||||
/// </summary>
|
||||
public static ReadOnlyCollection<TypeDeclaration> FindTypeDeclarations(this INode astTree) |
||||
{ |
||||
var findVisitor = new FindTypeDeclarationsVisitor(); |
||||
astTree.AcceptVisitor(findVisitor, null); |
||||
return findVisitor.Declarations; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Martin Konicek" email="martin.konicek@gmail.com"/>
|
||||
// <version>$Revision: $</version>
|
||||
// </file>
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Collections.ObjectModel; |
||||
using ICSharpCode.NRefactory.Ast; |
||||
using ICSharpCode.NRefactory.Visitors; |
||||
|
||||
namespace SharpRefactoring.ContextActions |
||||
{ |
||||
/// <summary>
|
||||
/// Finds TypeDeclarations in AST tree.
|
||||
/// </summary>
|
||||
public class FindTypeDeclarationsVisitor : AbstractAstVisitor |
||||
{ |
||||
List<TypeDeclaration> foundDeclarations; |
||||
public ReadOnlyCollection<TypeDeclaration> Declarations { get; private set; } |
||||
|
||||
public FindTypeDeclarationsVisitor() |
||||
{ |
||||
this.foundDeclarations = new List<TypeDeclaration>(); |
||||
this.Declarations = this.foundDeclarations.AsReadOnly(); |
||||
} |
||||
|
||||
public override object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data) |
||||
{ |
||||
this.foundDeclarations.Add(typeDeclaration); |
||||
return base.VisitTypeDeclaration(typeDeclaration, data); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,101 @@
@@ -0,0 +1,101 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Martin Konicek" email="martin.konicek@gmail.com"/>
|
||||
// <version>$Revision: $</version>
|
||||
// </file>
|
||||
using System; |
||||
using ICSharpCode.NRefactory; |
||||
using ICSharpCode.NRefactory.Ast; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver; |
||||
using ICSharpCode.SharpDevelop.Editor; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Helper class for <see cref="IContextActionsProvider.GetAvailableActions"></see>.
|
||||
/// Never keep long-lived references to this class
|
||||
/// - the AST serves as one-time cache and does not get updated when editor text changes.
|
||||
/// </summary>
|
||||
public class EditorASTProvider |
||||
{ |
||||
public ITextEditor Editor { get; private set; } |
||||
public SnippetParser snippetParser { get; private set; } |
||||
public NRefactoryResolver resolver { get; private set; } |
||||
|
||||
public EditorASTProvider(ITextEditor editor) |
||||
{ |
||||
if (editor == null) |
||||
throw new ArgumentNullException("editor"); |
||||
this.Editor = editor; |
||||
this.snippetParser = GetSnippetParser(editor); |
||||
this.resolver = GetResolver(editor); |
||||
} |
||||
|
||||
INode currentLineAST; |
||||
public INode CurrentLineAST |
||||
{ |
||||
get |
||||
{ |
||||
if (currentLineAST != null) |
||||
return currentLineAST; |
||||
if (snippetParser == null) |
||||
return null; |
||||
try { |
||||
var currentLine = Editor.Document.GetLine(Editor.Caret.Line); |
||||
return (currentLineAST = snippetParser.Parse(currentLine.Text)); |
||||
} |
||||
catch { |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
INode currentMemberAST; |
||||
public INode CurrentMemberAST |
||||
{ |
||||
get |
||||
{ |
||||
if (resolver == null) |
||||
return null; |
||||
if (currentMemberAST != null) |
||||
return currentMemberAST; |
||||
try { |
||||
resolver.Initialize(ParserService.GetParseInformation(Editor.FileName), Editor.Caret.Line, Editor.Caret.Column); |
||||
return (currentMemberAST = resolver.ParseCurrentMember(Editor.Document.Text)); |
||||
} |
||||
catch { |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
SnippetParser GetSnippetParser(ITextEditor editor) |
||||
{ |
||||
var lang = GetEditorLanguage(editor); |
||||
if (lang != null) { |
||||
return new SnippetParser(lang.Value); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
NRefactoryResolver GetResolver(ITextEditor editor) |
||||
{ |
||||
if (editor == null || editor.Language == null) |
||||
return null; |
||||
return new NRefactoryResolver(editor.Language.Properties); |
||||
} |
||||
|
||||
SupportedLanguage? GetEditorLanguage(ITextEditor editor) |
||||
{ |
||||
if (editor == null || editor.Language == null) |
||||
return null; |
||||
if (editor.Language.Properties == LanguageProperties.CSharp) |
||||
return SupportedLanguage.CSharp; |
||||
if (editor.Language.Properties == LanguageProperties.VBNet) |
||||
return SupportedLanguage.VBNet; |
||||
return null; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue