Skip to content

Commit

Permalink
Creating a new view with an invalid class name now shows nicer error
Browse files Browse the repository at this point in the history
Fixes #285
  • Loading branch information
tznind committed Jan 8, 2024
1 parent 4d710a2 commit b827967
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/SourceCodeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public SourceCodeFile(string path)
/// </summary>
public FileInfo DesignerFile { get; }

/// <summary>
/// Returns the class name for the file e.g. for /SomeDir/MyClass.cs it will return MyClass
/// </summary>
public string ClassName => Path.GetFileNameWithoutExtension(CsFile.Name);

/// <summary>
/// Returns the .Designer.cs file for the given class file.
/// Returns a reference even if that file does not exist.
Expand Down
5 changes: 5 additions & 0 deletions src/ToCode/CodeDomArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public static string MakeValidFieldName(string? name, bool isPublic = false)
return name;
}

internal static bool IsValidIdentifier(string name)
{
return cSharpCodeProvider.IsValidIdentifier(name);
}

/// <summary>
/// Returns a unique field name based on the passed value.
/// Removes non word characters and applies a numerical
Expand Down
2 changes: 1 addition & 1 deletion src/ToCode/ViewToCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Design GenerateNewView(FileInfo csFilePath, string namespaceName, Type vi

var sourceFile = new SourceCodeFile(csFilePath);

var className = Path.GetFileNameWithoutExtension(sourceFile.CsFile.Name);
var className = sourceFile.ClassName;

var csharpCode = GetGenerateNewViewCode(className, namespaceName);
File.WriteAllText(sourceFile.CsFile.FullName, csharpCode);
Expand Down
23 changes: 23 additions & 0 deletions src/UI/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,22 @@ private void BuildRootMenu()
break;
}
}


if (e == this.keyMap.New)
{
this.New();
}

if (e == this.keyMap.ShowHelp)
{
this.ShowHelp();
}

if (e == this.keyMap.Open)
{
this.Open();
}
};

this.Add(this.rootCommandsListView);
Expand Down Expand Up @@ -956,6 +972,13 @@ private void New()
// Check if we are about to overwrite some files
// and if so warn the user
var files = new SourceCodeFile(file);

if(!CodeDomArgs.IsValidIdentifier(files.ClassName))
{
ChoicesDialog.Query("Invalid Name",$"Invalid class name '{files.ClassName}'","Ok");
return;
}

var sb = new StringBuilder();

if (files.CsFile.Exists)
Expand Down

0 comments on commit b827967

Please sign in to comment.