-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
regex form guess CSV params; fix multiline tb bug
1. the regex search form now automatically tries to guess the delimiter, eol and number of columns for a CSV file if the new auto_try_guess_csv_delim_newline setting is on. This is pretty slow, but there's room for improvement, and it makes life a lot easier. 2. Fix bug (introduced in last commit) where the Enter key does not add a new line in multiline textboxes.
- Loading branch information
1 parent
bfba863
commit 5a418d7
Showing
14 changed files
with
433 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
name: Continuous Integration | ||
|
||
on: | ||
push: | ||
paths-ignore: | ||
- 'docs/**' | ||
- '*.md' | ||
- '*.txt' | ||
- '*.PNG' | ||
- 'makerelease.bat' | ||
- 'testfiles/**' | ||
|
||
jobs: | ||
build: | ||
runs-on: windows-2022 | ||
strategy: | ||
max-parallel: 4 | ||
matrix: | ||
build_configuration: [Release, Debug] | ||
build_platform: [x64, x86] | ||
|
||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v4 | ||
|
||
- name: Add msbuild to PATH | ||
uses: microsoft/[email protected] | ||
|
||
- name: MSBuild of solution | ||
run: msbuild JsonToolsNppPlugin/JsonToolsNppPlugin.sln /p:configuration="${{ matrix.build_configuration }}" /p:platform="${{ matrix.build_platform }}" /m /verbosity:minimal | ||
|
||
- name: Archive artifacts for x64 | ||
if: matrix.build_platform == 'x64' && matrix.build_configuration == 'Release' | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: plugin_dll_x64 | ||
path: JsonToolsNppPlugin\bin\${{ matrix.build_configuration }}-x64\JsonTools.dll | ||
|
||
- name: Archive artifacts for x86 | ||
if: matrix.build_platform == 'x86' && matrix.build_configuration == 'Release' | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: plugin_dll_x86 | ||
path: JsonToolsNppPlugin\bin\${{ matrix.build_configuration }}\JsonTools.dll | ||
|
||
name: Continuous Integration | ||
|
||
on: | ||
push: | ||
paths-ignore: | ||
- 'docs/**' | ||
- '*.md' | ||
- '*.txt' | ||
- '*.PNG' | ||
- 'makerelease.bat' | ||
- 'testfiles/**' | ||
|
||
jobs: | ||
build: | ||
runs-on: windows-2022 | ||
strategy: | ||
max-parallel: 4 | ||
matrix: | ||
build_configuration: [Release, Debug] | ||
build_platform: [x64, x86] | ||
|
||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v4 | ||
|
||
- name: Add msbuild to PATH | ||
uses: microsoft/[email protected] | ||
|
||
- name: MSBuild of solution | ||
run: msbuild JsonToolsNppPlugin/JsonToolsNppPlugin.sln /p:configuration="${{ matrix.build_configuration }}" /p:platform="${{ matrix.build_platform }}" /m /verbosity:minimal | ||
|
||
- name: Archive artifacts for x64 | ||
if: matrix.build_platform == 'x64' && matrix.build_configuration == 'Release' | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: plugin_dll_x64 | ||
path: JsonToolsNppPlugin\bin\${{ matrix.build_configuration }}-x64\JsonTools.dll | ||
|
||
- name: Archive artifacts for x86 | ||
if: matrix.build_platform == 'x86' && matrix.build_configuration == 'Release' | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: plugin_dll_x86 | ||
path: JsonToolsNppPlugin\bin\${{ matrix.build_configuration }}\JsonTools.dll | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Kbg.NppPluginNET.PluginInfrastructure; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace JSON_Tools.JSON_Tools | ||
{ | ||
public class CsvSniffer | ||
{ | ||
public const int DEFAULT_MAX_CHARS_TO_SNIFF = 1600; | ||
|
||
/// <summary> | ||
/// Attempt to parse text as an RFC 4180-compliant CSV file with delimiter delimiter, newline eol, and quote character '"'.<br></br> | ||
/// Each line, count how many columns there are. If there are two lines with different numbers of lines, or if all lines have one column, return -1.<br></br> | ||
/// If maxLinesToSniff lines are consumed before hitting maxCharsToSniff characters, | ||
/// and all the lines have the same number of columns, return that number of columns<br></br> | ||
/// If maxCharsToSniff characters are consumed, return -1 unless at least minLinesToDecide lines were consumed. | ||
/// </summary> | ||
/// <param name="eol">CR, CRLF, or LF</param> | ||
/// <param name="delimiter">the delimiter (e.g., ',' for a CSV file, '\t' for a TSV file)</param> | ||
/// <param name="maxLinesToSniff">consume no more than this many complete lines while sniffing</param> | ||
/// <param name="maxCharsToSniff">consume no more than this many characters while sniffing</param> | ||
/// <param name="minLinesToDecide">return -1 if fewer than this many complete lines were consumed</param> | ||
/// <returns>the number of columns in the file, or -1 if text does not appear to have that delimiter-eol combo</returns> | ||
public static int Sniff(string text, EndOfLine eol, char delimiter, char quote, int maxLinesToSniff = 16, int maxCharsToSniff = DEFAULT_MAX_CHARS_TO_SNIFF, int minLinesToDecide = 6) | ||
{ | ||
maxCharsToSniff = maxCharsToSniff > text.Length ? text.Length : maxCharsToSniff; | ||
int nColumns = -1; | ||
int nColumnsThisLine = 0; | ||
int matchStart = 0; | ||
int linesConsumed = 0; | ||
string delimStr = ArgFunction.CsvCleanChar(delimiter); | ||
string newline = eol == EndOfLine.CRLF ? "\r\n" : eol == EndOfLine.CR ? "\r" : "\n"; | ||
string escapedNewline = JNode.StrToString(newline, false); | ||
string newlineOrDelimiter = $"(?:{delimStr}|{escapedNewline}|\\z)"; | ||
string regexStr = ArgFunction.CsvColumnRegex(ArgFunction.CsvCleanChar(delimiter), new string(quote, 1)) + newlineOrDelimiter; | ||
Regex regex = new Regex(regexStr, RegexOptions.Compiled); | ||
while (matchStart < maxCharsToSniff) | ||
{ | ||
Match match = regex.Match(text, matchStart); | ||
if (!match.Success) | ||
return -1; | ||
nColumnsThisLine++; | ||
int matchEnd = matchStart + match.Length; | ||
if (matchEnd == matchStart) | ||
matchEnd++; | ||
bool atEndOfLine = matchEnd >= text.Length || match.Value.EndsWith(newline); | ||
if (atEndOfLine) | ||
{ | ||
if (nColumns == -1) // first line | ||
nColumns = nColumnsThisLine; | ||
else if (nColumns == 1 // a row has only one column | ||
|| (nColumns >= 0 && nColumnsThisLine != nColumns)) // two rows with different numbers of columns | ||
return -1; | ||
nColumnsThisLine = 0; | ||
linesConsumed++; | ||
if (linesConsumed == maxLinesToSniff) | ||
return nColumns; | ||
} | ||
matchStart = matchEnd; | ||
} | ||
if (linesConsumed < 2 // only one line is never enough to decide anything | ||
|| (matchStart < text.Length && linesConsumed < minLinesToDecide)) | ||
// we haven't consumed enough lines to be confident in our delimiter-eol combo | ||
// (unless we consumed the whole file, in which case we're fine) | ||
return -1; | ||
return nColumns; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.