Mangled code
In the simplistic case, open a source file that contains LF line endings.
Visual Studio prompts to translate the line endings to CR/LF. Respond by
clicking Yes. At this point all highlighting is shifted one character to the
left for every translated line ending (e.g. ReSharper apparently is not
aware the file was altered internally by Visual Studio). This by itself may
not be that big a deal since it seems to affect only highlighting.
The major issue is that using ReSharper to alter the code in any way in this
state results in a mangled source file. This can normally be mitigated by
opening the source file, allowing Visual Studio to translate the line
endings, and then immediately closing and saving the file to force ReSharper
to reparse the file. It isn't always this easy, unfortunately. Imagine one
or two of potentially a hundred source files have LF line endings that were
created by another developer using an odd editor and you refactor something
that just happens to require a change in one of those affected files. This
just happened to me.
While renaming a class, I was prompted to translate the line endings in some
other file that referenced this class. I responded Yes and everything seemed
OK. When I attempted to recompile the changes to run the unit tests, I
received a flurry of compiler errors that I had not received before. When I
browsed to the particular file Visual Studio had translated, I saw a mangled
mess. This is one of the original (untouched) classes in that file:
public IDirectedEnumerable Backwards()
{
MappedDirectedEnumerable retval = (MappedDirectedEnumerable)MemberwiseClone();
retval.directedenumerable = directedenumerable.Backwards();
return retval;
//If we made this classs non-abstract we could do
//return new
MappedDirectedCollectionValue(directedcollectionvalue.Backwards());;
}
This is the same class after the refactoring:
pub lic IDirectedEnumer > Backwards()
{
MappedDirectedEnumerable r = (MappedDirect erable)MemberwiseClone();
retval.dire umerable = directedenumerable.Backwards();
return retval;
//If we made th lass n-abstract we codo
//return new MappedDi ed tionVale]]>(directedcollectionvalue. {
s());;
}
Surprisingly, this is one of the more readable classes in that file.
I experimented with this and managed to get ReSharper to throw this
exception in some (but not all) instances:
JetBrains.Util.InternalErrorException: range.StartOffset should not break
CR-LF
at JetBrains.Util.InternalErrorException..ctor(String, Exception) in
c:\Agent\work\Server\be08dc054b0f1225\Platform\src\JetBrains.Util\src\InternalErrorException.cs:line
29 column 7
at JetBrains.Util.Logger.Fail(String) in
c:\Agent\work\Server\be08dc054b0f1225\Platform\src\JetBrains.Util\src\Logger\Logger.cs:line
135 column 5
at
JetBrains.VSAddin.Editor.VSDocumentOperationsHandler.ReplaceText(TextRange,
String) in
c:\Agent\work\Server\be08dc054b0f1225\src\VSAddin\src\Editor\DocumentOperationHandlers.cs:line
243 column 13
at
JetBrains.ReSharper.Editor.VSIntegration.VSDocument.DeleteText(TextRange) in
c:\Agent\work\Server\be08dc054b0f1225\src\VSAddin\src\Editor\VSDocument.cs:line
135 column 9
at
JetBrains.ReSharper.CodeInsight.ReformatCode.ReformatCodeActionBase.ApplyFormattedText(ISolution,
ReFormatterProjectFile) in
c:\Agent\work\Server\be08dc054b0f1225\src\CodeInsight\src\ReformatCode\ReformatCodeAction.cs:line
189 column 9
at
JetBrains.ReSharper.CodeInsight.ReformatCode.ReformatCodeActionBase.FormatFiles(Context,
ReformatCodeOptions) in
c:\Agent\work\Server\be08dc054b0f1225\src\CodeInsight\src\ReformatCode\ReformatCodeAction.cs:line
161 column 50
at
JetBrains.ReSharper.CodeInsight.ReformatCode.ReformatCodeActionBase.Execute(IDataContext,
DelegateExecute) in
c:\Agent\work\Server\be08dc054b0f1225\src\CodeInsight\src\ReformatCode\ReformatCodeAction.cs:line
85 column 13
at JetBrains.ActionManagement.ExecutableAction.Execute(IDataContext) in
c:\Agent\work\Server\be08dc054b0f1225\Platform\src\JetBrains.UI\src\ActionSystem\Action\ExecutableAction.cs:line
30 column 13
at
JetBrains.ActionManagement.ActionManagerBase.ExecuteAction(IExecutableAction,
IDataContext) in
c:\Agent\work\Server\be08dc054b0f1225\Platform\src\JetBrains.UI\src\ActionSystem\ActionManager\ActionManagerBase.cs:line
575 column 11
at
JetBrains.ActionManagement.ActionManagerBase.ExecuteAction(IExecutableAction)
in
c:\Agent\work\Server\be08dc054b0f1225\Platform\src\JetBrains.UI\src\ActionSystem\ActionManager\ActionManagerBase.cs:line
520 column 5
at JetBrains.ActionManagement.ActionManagerBase.ExecuteAction(String) in
c:\Agent\work\Server\be08dc054b0f1225\Platform\src\JetBrains.UI\src\ActionSystem\ActionManager\ActionManagerBase.cs:line
515 column 5
at JetBrains.ReSharper.VS.Customization.ReSharperPkg.Exec(Guid&, UInt32,
UInt32, IntPtr, IntPtr) in
c:\Agent\work\Server\be08dc054b0f1225\src\VS\src\Customization\ReSharperPkg.cs:line
44 column 6
at
Microsoft.VisualStudio.VSIP.Helper.Package.Microsoft.VisualStudio.OLE.Interop.IOleCommandTarget.Exec(Guid&,
UInt32, UInt32, IntPtr, IntPtr)
at JetBrains.Util.Logger.Fail(String messageText) in
c:\Agent\work\Server\be08dc054b0f1225\Platform\src\JetBrains.Util\src\Logger\Logger.cs:line
135
Please sign in to leave a comment.
Hello Lothan,
it is a known problem that after VS prompts to correct (unify) line endings,
ReSharper doesn't receive
any notification about change in the underlying file. Unfortunately, it looks
like a bug in Visual Studio.
So after applying such a conversion, ReSharper's and VS's images of the text
and don't match, and this will almost
certainly cause problems like ones you mention.
We understand that it is not always easy to convert line endings before refactorings,
as the user indeed may not be aware
that some files have them inconsistent. I guess we need to submit a bug to
Microsoft's tracker in hope it will get
attention one day.
Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
V>> )MemberwiseClone();
V>>
V>> )MemberwiseClone();
V>>