NHibernate Plugin

Hello JetBrainers,

a while ago I started working on a ReSharper plugin for NHibernate
mapping files. The plugin should support the rename refactoring so that
class names, property getter/setter, field names etc. are renamed in the
hbm.xml file too.

The problem is, that you changed the api. So now my plugin does not
compile anymore.

Main problem is in LanguageService. Old code is as follows:
LanguageServiceManager.Instance.RegisterLanguageService(
NHIBERNATE_LANGUAGE_TYPE, this);
LanguageServiceManager.Instance.BindExtension(
".hbm.xml", this);
CodeInsightServiceManager.Instance.RegisterService(
NHIBERNATE_LANGUAGE_TYPE, new NHibernateCodeInsightService());

The methods RegisterLanguageService, BindExtension and RegisterService
are not found. So can you give me an example for registering my language
service for hbm.xml files please?


After fixing this I need the hooks for rename refactoring. Please give
me some hints what I must do to tell ReSharper that my plugin needs to
do some work on renames.


Sincerely,
--
Stefan Lieser

EMail: stefan@lieser-online.de
Web: http://www.lieser-online.de

8 comments

Stefan,

Now we distinguish raw file type (called ProjectFileType) and PSI file type
(that corresponds to its structure). This is done for several reasons, one
of them is that there are C# files, that are generated from XMLs.

The example for Nant files is below:
Note, that language service has to be a shell component to be registered
automatically. You may use ProjectFileLanguageServiceManager and
LanguageServiceManager to access registered file types etc.

--- code
starts -



public class NAntFileLanguageService : IProjectFileLanguageService
{
private ReadOnlyCollection myExtensions = CollectionUtil.ReadonlyCollection(".build"); public static ProjectFileType NANT_BUILD_SCRIPT_FILE_TYPE = new ProjectFileType("NANT_BUILD_SCRIPT_FILE_TYPE", "NAnt build script"); public ProjectFileType LanguageType { get { return NANT_BUILD_SCRIPT_FILE_TYPE; } } public ReadOnlyCollection]]> Extensions { get { return
myExtensions; } }

public PsiLanguageType GetPsiLanguageType(IProjectFile file)
{
return file.LanguageType == NANT_BUILD_SCRIPT_FILE_TYPE ?
NAntLanguageService.NANT_BUILD_SCRIPT : PsiLanguageType.UNKNOWN;
}

public PsiLanguageType GetPsiLanguageType(ProjectFileType languageType)
{
return languageType == NANT_BUILD_SCRIPT_FILE_TYPE ?
NAntLanguageService.NANT_BUILD_SCRIPT : PsiLanguageType.UNKNOWN;
}

private class MyLexerFactory : ILexerFactory
{
public IRawLexer CreateLexer(char[] buffer)
{
return new XmlLexerGenerated(buffer);
}
}

public ILexerFactory CreateLexer(ProjectFileType languageType, char[]
buffer)
{
if (languageType != NANT_BUILD_SCRIPT_FILE_TYPE)
return null;

return new MyLexerFactory();
}

private class NAntWordIndexLanguageProvider : IWordIndexLanguageProvider
{
public bool CaseSensitiveIdentifiers
{
get { return true; }
}

public bool IsIdentifierFirstLetter(char ch)
{
if (char.IsLetter(ch))
{
return true;
}

return false;
}

public bool IsIdentifierSecondLetter(char ch)
{
if (char.IsLetter(ch) || char.IsDigit(ch) || ch == '_' || ch == '.'
|| ch == '-')
{
return true;
}

return false;
}
}

public IWordIndexLanguageProvider WordIndexLanguageProvider { get {
return new NAntWordIndexLanguageProvider(); } }
}




public class NAntLanguageService : LanguageService, IShellComponent
{
public static PsiLanguageType NANT_BUILD_SCRIPT = new
PsiLanguageType("NANT_BUILD_SCRIPT", "NAnt build script");

... some code missing
}


0

Hello Sergey,

Sergey V. Coox (JetBrains) schrieb:

Stefan,

Now we distinguish raw file type (called ProjectFileType) and PSI file type
(that corresponds to its structure). This is done for several reasons, one
of them is that there are C# files, that are generated from XMLs.

The example for Nant files is below:
Note, that language service has to be a shell component to be registered
automatically. You may use ProjectFileLanguageServiceManager and
LanguageServiceManager to access registered file types etc.


thanks for the info! Plugin is loaded and .hbm.xml files are detected
(at least the error stripe on the right side of the editor window is
displayed).

I think I have to implement IReference on my Psi elements which need
rename support. Can you give me some hints about that topic please?


Sincerely,
--
Stefan Lieser

EMail: stefan@lieser-online.de
Web: http://www.lieser-online.de

0

Hello Sergey,

I have more questions. The parser wasn't called yet. Debugging shows me
the following:

public PsiLanguageType GetPsiLanguageType(IProjectFile file) {
return file.LanguageType == NHIBERNATE_MAPPING_FILE_TYPE ?
NHibernateLanguageService.NHIBERNATE_LANGUAGE_TYPE :
PsiLanguageType.UNKNOWN;
}

file.LanguageType is always UNKNOWN, even for .hbm.xml files. So my
method never returns NHibernateLanguageService.NHIBERNATE_LANGUAGE_TYPE
and the parser is not called.

Btw. the file extension is set like the following:
private ReadOnlyCollection]]> myExtensions =
CollectionUtil.ReadonlyCollection(".hbm.xml");
Is it ok to have an extension with a period inside?



public PsiLanguageType GetPsiLanguageType(ProjectFileType languageType) {
return languageType == NHIBERNATE_MAPPING_FILE_TYPE ?
NHibernateLanguageService.NHIBERNATE_LANGUAGE_TYPE :
PsiLanguageType.UNKNOWN;
}

The methods parameter 'languageType' is always LanguageType.UNKNOWN
Should I always return NHibernateLanguageService.NHIBERNATE_LANGUAGE_TYPE?


Sincerely,
--
Stefan Lieser

EMail: stefan@lieser-online.de
Web: http://www.lieser-online.de

0

Stefan,

Unfortunately, we use Path.GetExtension which leads to considering an
extension a part of a name, that starts from the last period.
You may subscribe to .xml file types but check the file name for the "long"
extension in GetPsiFileLanguageType. That's not an elegant solution, but
right now I've no better one.

--
Sergey V. Coox
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0

Hello Sergey,

Sergey V. Coox (JetBrains) schrieb:

Stefan,

Unfortunately, we use Path.GetExtension which leads to considering an
extension a part of a name, that starts from the last period.
You may subscribe to .xml file types but check the file name for the "long"
extension in GetPsiFileLanguageType. That's not an elegant solution, but
right now I've no better one.


done that.

Can you please answer my other questions? The parser now runs for
.hbm.xml files. What must I do to get IReference calls for rename
refactoring support?


Sincerely,
Stefan Lieser

0

Hello Stefan,

To support rename you basically need the following things: provide SetName
and DeclaredElement implementation for your declarations to change their
names during rename and get correct declared elements; and implement Bind
method in your references to change their name according to declared element
it's being bound to.

Oleg Stepanov
Software Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Hello Sergey,

Sergey V. Coox (JetBrains) schrieb:

>> Stefan,
>>
>> Now we distinguish raw file type (called ProjectFileType) and PSI
>> file type (that corresponds to its structure). This is done for
>> several reasons, one of them is that there are C# files, that are
>> generated from XMLs.
>>
>> The example for Nant files is below:
>> Note, that language service has to be a shell component to be
>> registered
>> automatically. You may use ProjectFileLanguageServiceManager and
>> LanguageServiceManager to access registered file types etc.

thanks for the info! Plugin is loaded and .hbm.xml files are detected
(at least the error stripe on the right side of the editor window is
displayed).

I think I have to implement IReference on my Psi elements which need
rename support. Can you give me some hints about that topic please?

Sincerely,

EMail: stefan@lieser-online.de
Web: http://www.lieser-online.de



0

I wondered if you were still working on the plugin because I'd be very interested in using it.

0

Hi,

I'm still waiting for a feature complete R# 4.0 because I don't like to
program against a changing api ;)


Regards,
Stefan Lieser
--
http://www.lieser-online.de

Colin Jack schrieb:

I wondered if you were still working on the plugin because I'd be very interested in using it.

0

Please sign in to leave a comment.