Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PTRun][OneNote] Improve the OneNote plugin #36813

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Refactor creating new OneNote item results
  • Loading branch information
Odotocodot committed Jan 18, 2025
commit 1554fddd1c3172d605aff80798c11a55a6b1fea3
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ internal static bool OpenItemInOneNote(this IOneNoteItem item)
/// <summary>
/// Brings OneNote to the foreground and restores it if minimized.
/// </summary>
private static void ShowOneNote()
internal static void ShowOneNote()
{
using var process = Process.GetProcessesByName("onenote").FirstOrDefault();
if (process?.MainWindowHandle != null)
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
@@ -31,7 +32,6 @@ public class ResultCreator
private static readonly CompositeFormat CreateSection = CompositeFormat.Parse(Resources.CreateSection);
private static readonly CompositeFormat CreateSectionGroup = CompositeFormat.Parse(Resources.CreateSectionGroup);
private static readonly CompositeFormat CreateNotebook = CompositeFormat.Parse(Resources.CreateNotebook);
private static readonly CompositeFormat Location = CompositeFormat.Parse(Resources.Location);
private static readonly CompositeFormat Path = CompositeFormat.Parse(Resources.Path);
private static readonly CompositeFormat LastModified = CompositeFormat.Parse(Resources.LastModified);
private static readonly CompositeFormat SectionNamesCannotContain = CompositeFormat.Parse(Resources.SectionNamesCannotContain);
@@ -72,7 +72,7 @@ private string GetTitle(IOneNoteItem item, List<int>? highlightData)
return title;
}

private string GetQueryTextDisplay(IOneNoteItem? parent)
private static string GetQueryTextDisplay(IOneNoteItem? parent)
{
return parent is null
? $"{Keywords.NotebookExplorer}"
@@ -231,124 +231,84 @@ internal Result CreateRecentPageResult(OneNotePage page)
return result;
}

internal Result CreateNewPageResult(string newPageName, OneNoteSection section)
private Result CreateNewOneNoteItemResult(string newItemName, IOneNoteItem? parent, CompositeFormat titleFormat, ImmutableArray<char> invalidCharacters, CompositeFormat subTitleFormat, string iconPath, Action createItemAction)
{
newPageName = newPageName.Trim();
return new Result
{
Title = string.Format(CultureInfo.CurrentCulture, CreatePage, newPageName),
SubTitle = string.Format(CultureInfo.CurrentCulture, Path, GetNicePath(section) + PathSeparator + newPageName),
QueryTextDisplay = $"{GetQueryTextDisplay(section)}{newPageName}",
IcoPath = _iconProvider.NewPage,
Action = ResultAction(() =>
{
OneNoteApplication.CreatePage(section, newPageName, true);
return true;
}),
};
}
newItemName = newItemName.Trim();

internal Result CreateNewSectionResult(string newSectionName, IOneNoteItem parent)
{
newSectionName = newSectionName.Trim();
bool validTitle = OneNoteApplication.IsSectionNameValid(newSectionName);
bool validTitle = !string.IsNullOrWhiteSpace(newItemName) && !invalidCharacters.Any(newItemName.Contains);

string subTitle = parent == null
? OneNoteApplication.GetDefaultNotebookLocation() + System.IO.Path.DirectorySeparatorChar + newItemName
: GetNicePath(parent) + PathSeparator + newItemName;

return new Result
{
Title = string.Format(CultureInfo.CurrentCulture, CreateSection, newSectionName),
Title = string.Format(CultureInfo.CurrentCulture, titleFormat, newItemName),
SubTitle = validTitle
? string.Format(CultureInfo.CurrentCulture, Path, GetNicePath(parent) + PathSeparator + newSectionName)
: string.Format(CultureInfo.CurrentCulture, SectionNamesCannotContain, string.Join(' ', OneNoteApplication.InvalidSectionChars)),
QueryTextDisplay = $"{GetQueryTextDisplay(parent)}{newSectionName}",
IcoPath = _iconProvider.NewSection,
? string.Format(CultureInfo.CurrentCulture, Path, subTitle)
: string.Format(CultureInfo.CurrentCulture, subTitleFormat, string.Join(' ', invalidCharacters)),
QueryTextDisplay = $"{GetQueryTextDisplay(parent)}{newItemName}",
IcoPath = iconPath,
Action = ResultAction(() =>
{
if (!validTitle)
{
return false;
}

switch (parent)
{
case OneNoteNotebook notebook:
OneNoteApplication.CreateSection(notebook, newSectionName, true);
break;
case OneNoteSectionGroup sectionGroup:
OneNoteApplication.CreateSection(sectionGroup, newSectionName, true);
break;
default:
break;
}
createItemAction();

_context.API.ChangeQuery(_context.CurrentPluginMetadata.ActionKeyword, true);
OneNoteItemExtensions.ShowOneNote();
_context.API.ChangeQuery($"{GetQueryTextDisplay(parent)}{newItemName}", true);
return true;
}),
};
}

internal Result CreateNewSectionGroupResult(string newSectionGroupName, IOneNoteItem parent)
internal Result CreateNewPageResult(string newPageName, OneNoteSection section)
{
newSectionGroupName = newSectionGroupName.Trim();
bool validTitle = OneNoteApplication.IsSectionGroupNameValid(newSectionGroupName);
return CreateNewOneNoteItemResult(newPageName, section, CreatePage, [], SectionNamesCannotContain, _iconProvider.NewPage, () => OneNoteApplication.CreatePage(section, newPageName, true));
}

return new Result
internal Result CreateNewSectionResult(string newSectionName, IOneNoteItem parent)
{
return CreateNewOneNoteItemResult(newSectionName, parent, CreateSection, OneNoteApplication.InvalidSectionChars, SectionNamesCannotContain, _iconProvider.NewSection, () =>
{
Title = string.Format(CultureInfo.CurrentCulture, CreateSectionGroup, newSectionGroupName),
SubTitle = validTitle
? string.Format(CultureInfo.CurrentCulture, Path, GetNicePath(parent) + PathSeparator + newSectionGroupName)
: string.Format(CultureInfo.CurrentCulture, SectionGroupNamesCannotContain, string.Join(' ', OneNoteApplication.InvalidSectionGroupChars)),
QueryTextDisplay = $"{GetQueryTextDisplay(parent)}{newSectionGroupName}",
IcoPath = _iconProvider.NewSectionGroup,
Action = ResultAction(() =>
switch (parent)
{
if (!validTitle)
{
return false;
}

switch (parent)
{
case OneNoteNotebook notebook:
OneNoteApplication.CreateSectionGroup(notebook, newSectionGroupName, true);
break;
case OneNoteSectionGroup sectionGroup:
OneNoteApplication.CreateSectionGroup(sectionGroup, newSectionGroupName, true);
break;
default:
break;
}

_context.API.ChangeQuery(_context.CurrentPluginMetadata.ActionKeyword, true);
return true;
}),
};
case OneNoteNotebook notebook:
OneNoteApplication.CreateSection(notebook, newSectionName, true);
break;
case OneNoteSectionGroup sectionGroup:
OneNoteApplication.CreateSection(sectionGroup, newSectionName, true);
break;
default:
break;
}
});
}

internal Result CreateNewNotebookResult(string newNotebookName)
internal Result CreateNewSectionGroupResult(string newSectionGroupName, IOneNoteItem parent)
{
newNotebookName = newNotebookName.Trim();
bool validTitle = OneNoteApplication.IsNotebookNameValid(newNotebookName);

return new Result
return CreateNewOneNoteItemResult(newSectionGroupName, parent, CreateSectionGroup, OneNoteApplication.InvalidSectionGroupChars, SectionGroupNamesCannotContain, _iconProvider.NewSectionGroup, () =>
{
Title = string.Format(CultureInfo.CurrentCulture, CreateNotebook, newNotebookName),
SubTitle = validTitle
? string.Format(CultureInfo.CurrentCulture, Location, OneNoteApplication.GetDefaultNotebookLocation())
: string.Format(CultureInfo.CurrentCulture, NotebookNamesCannotContain, string.Join(' ', OneNoteApplication.InvalidNotebookChars)),
QueryTextDisplay = $"{GetQueryTextDisplay(null)}{newNotebookName}",
IcoPath = _iconProvider.NewNotebook,
Action = ResultAction(() =>
switch (parent)
{
if (!validTitle)
{
return false;
}
case OneNoteNotebook notebook:
OneNoteApplication.CreateSectionGroup(notebook, newSectionGroupName, true);
break;
case OneNoteSectionGroup sectionGroup:
OneNoteApplication.CreateSectionGroup(sectionGroup, newSectionGroupName, true);
break;
default:
break;
}
});
}

OneNoteApplication.CreateNotebook(newNotebookName, true);
_context.API.ChangeQuery(_context.CurrentPluginMetadata.ActionKeyword, true);
return true;
}),
};
internal Result CreateNewNotebookResult(string newNotebookName)
{
return CreateNewOneNoteItemResult(newNotebookName, null, CreateNotebook, OneNoteApplication.InvalidNotebookChars, NotebookNamesCannotContain, _iconProvider.NewNotebook, () => OneNoteApplication.CreateNotebook(newNotebookName, true));
}

internal List<ContextMenuResult> LoadContextMenu(Result selectedResult)

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
@@ -215,9 +215,6 @@
<value>Create notebook: "{0}"</value>
<comment>0 is a user typed name</comment>
</data>
<data name="Location" xml:space="preserve">
<value>Location: {0}</value>
</data>
<data name="NotebookNamesCannotContain" xml:space="preserve">
<value>Notebook names cannot contain: {0}</value>
</data>