mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 19:57:57 +01:00
[PreviewPane][Monaco]Format json/xml before rendering (#19980)
This commit is contained in:
committed by
GitHub
parent
e7d3aadec3
commit
16c7a22410
1
.github/actions/spell-check/expect.txt
vendored
1
.github/actions/spell-check/expect.txt
vendored
@@ -836,6 +836,7 @@ IFilter
|
|||||||
ifndef
|
ifndef
|
||||||
IFolder
|
IFolder
|
||||||
IFormat
|
IFormat
|
||||||
|
IFormatter
|
||||||
ifstream
|
ifstream
|
||||||
IGraph
|
IGraph
|
||||||
iid
|
iid
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
namespace Microsoft.PowerToys.PreviewHandler.Monaco.Formatters
|
||||||
|
{
|
||||||
|
public interface IFormatter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the language to which the formatter is applied
|
||||||
|
/// </summary>
|
||||||
|
string LangSet { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Format the value
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to format</param>
|
||||||
|
/// <returns>The value formatted</returns>
|
||||||
|
string Format(string value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
namespace Microsoft.PowerToys.PreviewHandler.Monaco.Formatters
|
||||||
|
{
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
public class JsonFormatter : IFormatter
|
||||||
|
{
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public string LangSet => "json";
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public string Format(string value)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(value))
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var jDocument = JsonDocument.Parse(value, new JsonDocumentOptions { CommentHandling = JsonCommentHandling.Skip }))
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize(jDocument, new JsonSerializerOptions { WriteIndented = true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
namespace Microsoft.PowerToys.PreviewHandler.Monaco.Formatters
|
||||||
|
{
|
||||||
|
using System.Text;
|
||||||
|
using System.Xml;
|
||||||
|
|
||||||
|
public class XmlFormatter : IFormatter
|
||||||
|
{
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public string LangSet => "xml";
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public string Format(string value)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(value))
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var xmlDocument = new XmlDocument();
|
||||||
|
xmlDocument.LoadXml(value);
|
||||||
|
|
||||||
|
var stringBuilder = new StringBuilder();
|
||||||
|
var xmlWriterSettings = new XmlWriterSettings()
|
||||||
|
{
|
||||||
|
OmitXmlDeclaration = xmlDocument.FirstChild.NodeType != XmlNodeType.XmlDeclaration,
|
||||||
|
Indent = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
using (var xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings))
|
||||||
|
{
|
||||||
|
xmlDocument.Save(xmlWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringBuilder.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,14 +3,16 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Common;
|
using Common;
|
||||||
|
using Microsoft.PowerToys.PreviewHandler.Monaco.Formatters;
|
||||||
using Microsoft.PowerToys.PreviewHandler.Monaco.Helpers;
|
using Microsoft.PowerToys.PreviewHandler.Monaco.Helpers;
|
||||||
using Microsoft.PowerToys.PreviewHandler.Monaco.Properties;
|
using Microsoft.PowerToys.PreviewHandler.Monaco.Properties;
|
||||||
using Microsoft.Web.WebView2.Core;
|
using Microsoft.Web.WebView2.Core;
|
||||||
@@ -26,6 +28,15 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly Settings _settings = new Settings();
|
private readonly Settings _settings = new Settings();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Formatters applied before rendering the preview
|
||||||
|
/// </summary>
|
||||||
|
private readonly IReadOnlyCollection<IFormatter> _formatters = new List<IFormatter>
|
||||||
|
{
|
||||||
|
new JsonFormatter(),
|
||||||
|
new XmlFormatter(),
|
||||||
|
}.AsReadOnly();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Saves if the user already navigated to the index page
|
/// Saves if the user already navigated to the index page
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -367,6 +378,23 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
|
|||||||
{
|
{
|
||||||
Logger.LogInfo("Starting reading requested file");
|
Logger.LogInfo("Starting reading requested file");
|
||||||
var fileContent = fileReader.ReadToEnd();
|
var fileContent = fileReader.ReadToEnd();
|
||||||
|
|
||||||
|
if (_settings.TryFormat)
|
||||||
|
{
|
||||||
|
var formatter = _formatters.SingleOrDefault(f => f.LangSet == _vsCodeLangSet);
|
||||||
|
if (formatter != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
fileContent = formatter.Format(fileContent);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.LogError($"Failed to apply formatting to {filePath}", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fileReader.Close();
|
fileReader.Close();
|
||||||
_base64FileCode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(fileContent));
|
_base64FileCode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(fileContent));
|
||||||
Logger.LogInfo("Reading requested file ended");
|
Logger.LogInfo("Reading requested file ended");
|
||||||
|
|||||||
@@ -37,6 +37,26 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether to try formatting the file. Set by PT settings.
|
||||||
|
/// </summary>
|
||||||
|
public bool TryFormat
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return moduleSettings.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName).Properties.MonacoPreviewTryFormat;
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException)
|
||||||
|
{
|
||||||
|
// Couldn't read the settings.
|
||||||
|
// Assume default of false.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Max file size for displaying (in bytes).
|
/// Max file size for displaying (in bytes).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -99,6 +99,23 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool monacoPreviewTryFormat;
|
||||||
|
|
||||||
|
[JsonPropertyName("monaco-previewer-toggle-try-format")]
|
||||||
|
[JsonConverter(typeof(BoolPropertyJsonConverter))]
|
||||||
|
public bool MonacoPreviewTryFormat
|
||||||
|
{
|
||||||
|
get => monacoPreviewTryFormat;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value != monacoPreviewTryFormat)
|
||||||
|
{
|
||||||
|
LogTelemetryEvent(value);
|
||||||
|
monacoPreviewTryFormat = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private bool enablePdfPreview;
|
private bool enablePdfPreview;
|
||||||
|
|
||||||
[JsonPropertyName("pdf-previewer-toggle-setting")]
|
[JsonPropertyName("pdf-previewer-toggle-setting")]
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
|||||||
_mdRenderIsEnabled = Settings.Properties.EnableMdPreview;
|
_mdRenderIsEnabled = Settings.Properties.EnableMdPreview;
|
||||||
_monacoRenderIsEnabled = Settings.Properties.EnableMonacoPreview;
|
_monacoRenderIsEnabled = Settings.Properties.EnableMonacoPreview;
|
||||||
_monacoWrapText = Settings.Properties.EnableMonacoPreviewWordWrap;
|
_monacoWrapText = Settings.Properties.EnableMonacoPreviewWordWrap;
|
||||||
|
_monacoPreviewTryFormat = Settings.Properties.MonacoPreviewTryFormat;
|
||||||
_pdfRenderIsEnabled = Settings.Properties.EnablePdfPreview;
|
_pdfRenderIsEnabled = Settings.Properties.EnablePdfPreview;
|
||||||
_gcodeRenderIsEnabled = Settings.Properties.EnableGcodePreview;
|
_gcodeRenderIsEnabled = Settings.Properties.EnableGcodePreview;
|
||||||
_pdfThumbnailIsEnabled = Settings.Properties.EnablePdfThumbnail;
|
_pdfThumbnailIsEnabled = Settings.Properties.EnablePdfThumbnail;
|
||||||
@@ -63,6 +64,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
|||||||
private bool _mdRenderIsEnabled;
|
private bool _mdRenderIsEnabled;
|
||||||
private bool _monacoRenderIsEnabled;
|
private bool _monacoRenderIsEnabled;
|
||||||
private bool _monacoWrapText;
|
private bool _monacoWrapText;
|
||||||
|
private bool _monacoPreviewTryFormat;
|
||||||
private bool _pdfRenderIsEnabled;
|
private bool _pdfRenderIsEnabled;
|
||||||
private bool _gcodeRenderIsEnabled;
|
private bool _gcodeRenderIsEnabled;
|
||||||
private bool _svgThumbnailIsEnabled;
|
private bool _svgThumbnailIsEnabled;
|
||||||
@@ -161,6 +163,24 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool MonacoPreviewTryFormat
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _monacoPreviewTryFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_monacoPreviewTryFormat != value)
|
||||||
|
{
|
||||||
|
_monacoPreviewTryFormat = value;
|
||||||
|
Settings.Properties.MonacoPreviewTryFormat = value;
|
||||||
|
RaisePropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool PDFRenderIsEnabled
|
public bool PDFRenderIsEnabled
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|||||||
@@ -2164,6 +2164,12 @@ From there, simply click on one of the supported files in the File Explorer and
|
|||||||
<data name="AlwaysOnTop_RoundCorners.Content" xml:space="preserve">
|
<data name="AlwaysOnTop_RoundCorners.Content" xml:space="preserve">
|
||||||
<value>Enable round corners</value>
|
<value>Enable round corners</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="FileExplorerPreview_ToggleSwitch_Monaco_Try_Format.Description" xml:space="preserve">
|
||||||
|
<value>Applies to json and xml. Files remain unchanged.</value>
|
||||||
|
</data>
|
||||||
|
<data name="FileExplorerPreview_ToggleSwitch_Monaco_Try_Format.Header" xml:space="preserve">
|
||||||
|
<value>Try to format the source for preview</value>
|
||||||
|
</data>
|
||||||
<data name="Run_ConflictingKeywordInfo.Title" xml:space="preserve">
|
<data name="Run_ConflictingKeywordInfo.Title" xml:space="preserve">
|
||||||
<value>Some characters and phrases may conflict with global queries of other plugins if you use them as activation command.</value>
|
<value>Some characters and phrases may conflict with global queries of other plugins if you use them as activation command.</value>
|
||||||
</data>
|
</data>
|
||||||
|
|||||||
@@ -57,6 +57,11 @@
|
|||||||
IsChecked="{x:Bind ViewModel.MonacoWrapText, Mode=TwoWay}"
|
IsChecked="{x:Bind ViewModel.MonacoWrapText, Mode=TwoWay}"
|
||||||
Margin="{StaticResource ExpanderSettingMargin}"
|
Margin="{StaticResource ExpanderSettingMargin}"
|
||||||
IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}" />
|
IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}" />
|
||||||
|
<Rectangle Style="{StaticResource ExpanderSeparatorStyle}" />
|
||||||
|
<controls:CheckBoxWithDescriptionControl x:Uid="FileExplorerPreview_ToggleSwitch_Monaco_Try_Format"
|
||||||
|
IsChecked="{x:Bind ViewModel.MonacoPreviewTryFormat, Mode=TwoWay}"
|
||||||
|
Margin="{StaticResource ExpanderSettingMargin}"
|
||||||
|
IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</controls:SettingExpander.Content>
|
</controls:SettingExpander.Content>
|
||||||
</controls:SettingExpander>
|
</controls:SettingExpander>
|
||||||
|
|||||||
Reference in New Issue
Block a user