Skip to content

Commit

Permalink
Cherry-pick #11080 (#11087)
Browse files Browse the repository at this point in the history
* [DYN-2988] Add custom fixer for instance.None (#11022)

* add custom fixer for instance.None

* cleanup

* cleanup

* add post build step to copy custom fixers to output directory

* add unit test

* review comments

* edit fixer code comments

* turn on anti-aliasing (#11068)

* update base images for image comparison tests after anti-aliasing fix

* Guard against parser errors detected while migrating python code (#11080)

* guard against parser errors detected while migrating python code

* transfer Error state between side-by-side and inline diff views

* revert whitespace in unchanged file

* disable accept button for error state

* update migration message

* address review comments
  • Loading branch information
aparajit-pratap authored Sep 2, 2020
1 parent 7ecbeb7 commit 3cd2591
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 24 deletions.
36 changes: 32 additions & 4 deletions src/PythonMigrationViewExtension/Controls/BaseDiffViewer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ResourceDictionary.MergedDictionaries>
<ui:SharedResourceDictionary Source="{x:Static ui:SharedDictionaryManager.DynamoModernDictionaryUri}" />
</ResourceDictionary.MergedDictionaries>
<viewModels:DiffStateToVisibilityConverter x:Key="DiffStateToVis" />
<DataTemplate DataType="{x:Type viewModels:InLineViewModel}">
<local:InLineControl DataContext="{Binding}" />
</DataTemplate>
Expand All @@ -32,14 +33,14 @@
<Style x:Key="HasDifferenceController"
TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentViewModel.HasChanges}"
Value="True">
<DataTrigger Binding="{Binding Path=CurrentViewModel.DiffState}"
Value="{x:Static viewModels:State.HasChanges}">
<Setter Property="Content"
Value="{Binding Path=CurrentViewModel}">
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentViewModel.HasChanges}"
Value="False">
<DataTrigger Binding="{Binding Path=CurrentViewModel.DiffState}"
Value="{x:Static viewModels:State.NoChanges}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
Expand All @@ -63,6 +64,31 @@
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentViewModel.DiffState}"
Value="{x:Static viewModels:State.Error}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Vertical"
VerticalAlignment="Center"
Width="400">
<TextBlock Text="{x:Static p:Resources.MigrationAssistantErrorStateHeader}"
TextAlignment="Center"
HorizontalAlignment="Center"
FontSize="24"
Foreground="{StaticResource WorkspaceTabHeaderActiveTextBrush}"
TextWrapping="Wrap" />
<TextBlock Text="{x:Static p:Resources.MigrationAssistantErrorStateMessage}"
TextAlignment="Center"
HorizontalAlignment="Center"
FontSize="14"
Foreground="{StaticResource WorkspaceTabHeaderActiveTextBrush}"
TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
Expand Down Expand Up @@ -108,6 +134,7 @@
HorizontalAlignment="Left"
Grid.Column="0">
<Button Name="DiffButton"
Visibility="{Binding Path=CurrentViewModel.DiffState, Converter={StaticResource DiffStateToVis}}"
Click="OnDiffButtonClick"
HorizontalAlignment="Right"
ToolTip="{x:Static p:Resources.DiffButtonTooltip}"
Expand All @@ -125,6 +152,7 @@
HorizontalAlignment="Right"
Grid.Column="1">
<Button x:Name="AcceptButton"
Visibility="{Binding Path=CurrentViewModel.DiffState, Converter={StaticResource DiffStateToVis}}"
Click="OnAcceptButtonClicked"
HorizontalAlignment="Right"
ToolTip="{x:Static p:Resources.AcceptButtonTooltip}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Windows;
using System.Windows.Data;

namespace Dynamo.PythonMigration.Differ
{
public class DiffStateToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
var state = (State)value;
if (state == State.Error)
return Visibility.Hidden;
}

return Visibility.Visible;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
namespace Dynamo.PythonMigration.Differ
{
public enum State
{
NoChanges,
HasChanges,
Error
}

public interface IDiffViewViewModel
{
ViewMode ViewMode { get; }
bool HasChanges { get; }
State DiffState { get; set; }
}

public enum ViewMode
Expand Down
20 changes: 17 additions & 3 deletions src/PythonMigrationViewExtension/Differ/InLineViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@ public class InLineViewModel : IDiffViewViewModel
{
public ViewMode ViewMode { get; set; }
public DiffPaneModel DiffModel { get; set; }
public bool HasChanges { get { return DiffModel.HasDifferences; } }
private bool HasChanges { get { return DiffModel.HasDifferences; } }

private State diffState;
public State DiffState
{
get
{
if (diffState == State.Error) return State.Error;

diffState = HasChanges ? State.HasChanges : State.NoChanges;

return diffState;
}
set { diffState = value; }
}

public InLineViewModel(SideBySideDiffModel diffModel)
{
this.ViewMode = ViewMode.Inline;
this.DiffModel = ConvertToInline(diffModel);
ViewMode = ViewMode.Inline;
DiffModel = ConvertToInline(diffModel);
}

/// <summary>
Expand Down
16 changes: 15 additions & 1 deletion src/PythonMigrationViewExtension/Differ/SideBySideViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ public class SideBySideViewModel : IDiffViewViewModel
public SideBySideDiffModel DiffModel { get; set; }
public DiffPaneModel AfterPane { get { return DiffModel.NewText; } }
public DiffPaneModel BeforePane { get { return DiffModel.OldText; } }
public bool HasChanges { get { return DiffModel.NewText.HasDifferences | DiffModel.OldText.HasDifferences; } }
private bool HasChanges { get { return DiffModel.NewText.HasDifferences | DiffModel.OldText.HasDifferences; } }

private State diffState;
public State DiffState
{
get
{
if (diffState == State.Error) return State.Error;

diffState = HasChanges ? State.HasChanges : State.NoChanges;

return diffState;
}
set { diffState = value; }
}

public SideBySideViewModel(SideBySideDiffModel diffModel)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Dynamo.Interfaces;
using Dynamo.PythonMigration.Controls;
using Dynamo.PythonMigration.Differ;
using Python.Runtime;
using PythonNodeModels;

namespace Dynamo.PythonMigration.MigrationAssistant
Expand Down Expand Up @@ -42,37 +43,49 @@ public IDiffViewViewModel CurrentViewModel

public PythonMigrationAssistantViewModel(PythonNode pythonNode, WorkspaceModel workspace, IPathManager pathManager, Version dynamoVersion)
{
this.PythonNode = pythonNode;
this.OldCode = pythonNode.Script;
PythonNode = pythonNode;
OldCode = pythonNode.Script;

this.workspace = workspace;
this.backupDirectory = pathManager.BackupDirectory;
backupDirectory = pathManager.BackupDirectory;
this.dynamoVersion = dynamoVersion;

MigrateCode();
try
{
MigrateCode();
}
catch (PythonException)
{
var sidebyside = new SideBySideDiffBuilder();
diffModel = sidebyside.BuildDiffModel(OldCode, OldCode, false);

SetSideBySideViewModel();

CurrentViewModel.DiffState = State.Error;
return;
}
SetSideBySideViewModel();

}

#region Code migration

private void MigrateCode()
{
this.NewCode = ScriptMigrator.MigrateCode(this.OldCode);
NewCode = ScriptMigrator.MigrateCode(OldCode);

var sidebyside = new SideBySideDiffBuilder();
this.diffModel = sidebyside.BuildDiffModel(this.OldCode, this.NewCode, false);
diffModel = sidebyside.BuildDiffModel(OldCode, NewCode, false);
}

/// <summary>
/// Replaces the code in the Python node with the code changes made by the Migration Assistant.
/// </summary>
public void ChangeCode()
{
if (!this.CurrentViewModel.HasChanges)
if (CurrentViewModel.DiffState == State.NoChanges)
{
if (this.PythonNode.Engine == PythonEngineVersion.CPython3)
return;
this.PythonNode.Engine = PythonEngineVersion.CPython3;
PythonNode.Engine = PythonEngineVersion.CPython3;
return;
}

Expand All @@ -85,7 +98,7 @@ public void ChangeCode()
}

SavePythonMigrationBackup();
this.PythonNode.MigrateCode(this.NewCode);
PythonNode.MigrateCode(this.NewCode);
}

#endregion
Expand Down Expand Up @@ -131,19 +144,17 @@ internal void ChangeViewModel(ViewMode viewMode)
case ViewMode.SideBySide:
SetSideBySideViewModel();
break;
default:
break;
}
}

private void SetSideBySideViewModel()
{
this.CurrentViewModel = new SideBySideViewModel(this.diffModel);
CurrentViewModel = new SideBySideViewModel(diffModel);
}

private void SetInlineViewModel()
{
this.CurrentViewModel = new InLineViewModel(this.diffModel);
CurrentViewModel = new InLineViewModel(diffModel);
}

#endregion
Expand Down
18 changes: 18 additions & 0 deletions src/PythonMigrationViewExtension/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ By clicking “Continue”, you are agreeing to implement the syntax changes to
<data name="MigrationAssistantNoChangesStateMessage" xml:space="preserve">
<value>Your code is ready to run with the Python 3 engine. Click accept and we'll switch to the Python 3 engine for you.</value>
</data>
<data name="MigrationAssistantErrorStateHeader" xml:space="preserve">
<value>There is an error in your code!</value>
</data>
<data name="MigrationAssistantErrorStateMessage" xml:space="preserve">
<value>Fix all errors before attempting code migration.</value>
</data>
<data name="PackagedCustomNodesHeader" xml:space="preserve">
<value>Packaged</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions src/PythonMigrationViewExtension/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ By clicking “Continue”, you are agreeing to implement the syntax changes to
<data name="MigrationAssistantNoChangesStateMessage" xml:space="preserve">
<value>Your code is ready to run with the Python 3 engine. Click accept and we'll switch to the Python 3 engine for you.</value>
</data>
<data name="MigrationAssistantErrorStateHeader" xml:space="preserve">
<value>There is an error in your code!</value>
</data>
<data name="MigrationAssistantErrorStateMessage" xml:space="preserve">
<value>Fix all errors before attempting code migration.</value>
</data>
<data name="PackagedCustomNodesHeader" xml:space="preserve">
<value>Packaged</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<Compile Include="Controls\SideBySideControl.xaml.cs">
<DependentUpon>SideBySideControl.xaml</DependentUpon>
</Compile>
<Compile Include="Differ\DiffStateToVisibilityConverter.cs" />
<Compile Include="Differ\IDiffViewViewModel.cs" />
<Compile Include="Differ\InLineViewModel.cs" />
<Compile Include="Differ\RichTextBoxHandler.cs" />
Expand Down

0 comments on commit 3cd2591

Please sign in to comment.