Skip to content

Commit 43e3cee

Browse files
committed
make custom extension mapping json option work
1 parent 0ce485b commit 43e3cee

4 files changed

Lines changed: 68 additions & 4 deletions

File tree

CodeStats/CodeStatsPackage.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,14 @@ private static void InitializeAsync()
108108
{
109109
currentPulse = new Pulse();
110110

111-
Logger.Info("Initialing settings form...");
112-
113111
// Load config file
114112
_CodeStatsConfigFile = new ConfigFile();
115113
GetSettings(true);
116114

117115
Logger.Debug("Loaded config");
118116

119117
LoadExtensionMapping();
118+
LoadCustomExtensionMapping();
120119

121120
// Check for updates
122121
Task.Run(() =>
@@ -242,6 +241,37 @@ private static void LoadExtensionMapping()
242241
}
243242
}
244243

244+
private static void LoadCustomExtensionMapping()
245+
{
246+
try
247+
{
248+
// get path of plugin configuration
249+
StringBuilder sbConfigFilePath = new StringBuilder(Win32.MAX_PATH);
250+
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbConfigFilePath);
251+
string configFilePath = sbConfigFilePath.ToString();
252+
253+
string customExtensionMappingFilePath = ConfigFile.GetCustomExtensionMappingFilePath();
254+
255+
StreamReader _textStreamReader;
256+
string customExtensionMappingJson;
257+
var serializer = new JavaScriptSerializer();
258+
259+
_textStreamReader = File.OpenText(customExtensionMappingFilePath);
260+
customExtensionMappingJson = _textStreamReader.ReadToEnd();
261+
customExtensionMapping = serializer.Deserialize<Dictionary<string, string>>(customExtensionMappingJson);
262+
263+
Logger.Debug("Loaded custom extension mapping");
264+
}
265+
catch (FileNotFoundException)
266+
{
267+
Logger.Debug("Custom extension mapping JSON file does not exist");
268+
}
269+
catch (Exception ex)
270+
{
271+
Logger.Error("Error loading custom extension mappings!", ex);
272+
}
273+
}
274+
245275
internal static void SetToolBarIcon()
246276
{
247277
// TODO: Maybe check out https://docs.microsoft.com/en-us/windows/desktop/controls/embed-nonbutton-controls-in-toolbars to create counter within toolbar as alternative?
@@ -601,6 +631,22 @@ public static string GetCurrentLanguage() // Code::Stats
601631
{
602632
switch (detectionMethod)
603633
{
634+
case Constants.DetectionType.CUSTOM_MAPPING:
635+
if (!String.IsNullOrWhiteSpace(extension))
636+
{
637+
try
638+
{
639+
language = customExtensionMapping[extension];
640+
found = true;
641+
}
642+
catch (KeyNotFoundException)
643+
{
644+
Logger.Debug("Extension \"" + extension + "\" is not found in custom extension mappings file.");
645+
found = false;
646+
}
647+
}
648+
break;
649+
604650
case Constants.DetectionType.EXTENSION_MAPPING:
605651
if (!String.IsNullOrWhiteSpace(extension))
606652
{
@@ -754,6 +800,7 @@ private static void SettingsPopup()
754800
private static void SettingsFormOnConfigSaved(object sender, EventArgs eventArgs)
755801
{
756802
GetSettings();
803+
LoadCustomExtensionMapping();
757804
_settingsForm.OnConfigSaved -= SettingsFormOnConfigSaved;
758805
_settingsForm.Dispose();
759806
_settingsForm = null;

CodeStats/ConfigFile.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,14 @@ static string GetConfigFilePath()
172172

173173
return Path.Combine(iniFilePath, "CodeStats.ini");
174174
}
175+
176+
public static string GetCustomExtensionMappingFilePath()
177+
{
178+
StringBuilder sbConfigFilePath = new StringBuilder(Win32.MAX_PATH);
179+
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbConfigFilePath);
180+
string configFilePath = sbConfigFilePath.ToString();
181+
182+
return Path.Combine(configFilePath, "CodeStatsCustomExtensionMapping.json");
183+
}
175184
}
176185
}

CodeStats/Forms/SettingsForm.Designer.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodeStats/Forms/SettingsForm.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Windows.Forms;
45
using static CodeStats.Constants;
56

@@ -37,7 +38,7 @@ private void SettingsForm_Load(object sender, EventArgs e)
3738
}
3839

3940
chkUseExtensionMapping.Checked = _CodeStatsConfigFile.UseExtensionMapping;
40-
chkUseExtensionMapping.Checked = _CodeStatsConfigFile.UseLexerLanguage;
41+
chkUseLexerLanguage.Checked = _CodeStatsConfigFile.UseLexerLanguage;
4142
radioDetectionPriority_extensionMapping.Checked = _CodeStatsConfigFile.DetectionPriority == Constants.DetectionType.EXTENSION_MAPPING;
4243
radioDetectionPriority_lexerLanguage.Checked = _CodeStatsConfigFile.DetectionPriority == Constants.DetectionType.LEXER_LANGUAGE;
4344
chkUseCustomMapping.Checked = _CodeStatsConfigFile.UseCustomMapping;
@@ -102,7 +103,7 @@ private void btnOk_Click(object sender, EventArgs e)
102103

103104
LanguageDetectionUIRefresh();
104105
_CodeStatsConfigFile.UseExtensionMapping = chkUseExtensionMapping.Checked;
105-
_CodeStatsConfigFile.UseLexerLanguage = chkUseExtensionMapping.Checked;
106+
_CodeStatsConfigFile.UseLexerLanguage = chkUseLexerLanguage.Checked;
106107
_CodeStatsConfigFile.DetectionPriority = !radioDetectionPriority_extensionMapping.Checked ? Constants.DetectionType.LEXER_LANGUAGE : Constants.DetectionType.EXTENSION_MAPPING;
107108
_CodeStatsConfigFile.UseCustomMapping = chkUseCustomMapping.Checked;
108109
_CodeStatsConfigFile.RefreshDetectionOrder();
@@ -219,5 +220,11 @@ private void LanguageDetectionUIRefresh(object sender, EventArgs e)
219220
{
220221
LanguageDetectionUIRefresh();
221222
}
223+
224+
private void btnOpenCustomMappingFile_Click(object sender, EventArgs e)
225+
{
226+
string path = ConfigFile.GetCustomExtensionMappingFilePath();
227+
Process.Start("notepad.exe", path);
228+
}
222229
}
223230
}

0 commit comments

Comments
 (0)