Skip to content

Commit 472f190

Browse files
committed
Basic editor.replace2 functionality
Replacements working for simple strings.
1 parent 93e3ae1 commit 472f190

21 files changed

Lines changed: 323 additions & 38 deletions

PythonScript.Tests/TestRunner.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,49 @@
33

44
#include "stdafx.h"
55
#include <gtest/gtest.h>
6+
#include "ReplaceEntry.h"
7+
#include "Replacer.h"
8+
9+
void deleteEntry(NppPythonScript::ReplaceEntry* entry)
10+
{
11+
delete entry;
12+
}
13+
14+
void runReplace()
15+
{
16+
17+
NppPythonScript::Replacer replacer;
18+
std::list<NppPythonScript::ReplaceEntry* > entries;
19+
bool moreEntries = replacer.startReplace("aaabbbaaabb", 12, "(b+)", "x$1x", entries);
20+
//ASSERT_EQ(2, entries.size());
21+
std::list<NppPythonScript::ReplaceEntry*>::const_iterator it = entries.begin();
22+
for_each(entries.begin(), entries.end(), deleteEntry);
23+
}
24+
625

726
int main(int argc, char* argv[])
827
{
28+
#ifdef _DEBUG
29+
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
30+
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
31+
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
32+
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
33+
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
34+
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
35+
_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
36+
_CrtMemState state;
37+
_CrtMemCheckpoint(&state);
38+
#endif
39+
940
::testing::InitGoogleTest(&argc, argv);
10-
return RUN_ALL_TESTS();
41+
RUN_ALL_TESTS();
42+
43+
44+
// runReplace();
45+
46+
#ifdef _DEBUG
47+
_CrtMemDumpAllObjectsSince(&state);
48+
#endif
49+
1150
}
1251

PythonScript.Tests/stdafx.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,14 @@
4141
#include <exception>
4242
#include <stdlib.h>
4343
#include <iterator>
44-
#include <algorithm>
44+
#include <algorithm>
45+
46+
#ifdef _DEBUG
47+
#define _CRTDBG_MAP_ALLOC
48+
#include <crtdbg.h>
49+
#ifndef DBG_NEW
50+
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
51+
#define new DBG_NEW
52+
#endif
53+
54+
#endif

PythonScript.Tests/tests/TestReplacer.cpp

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
12
#include "stdafx.h"
3+
4+
25
#include <gtest/gtest.h>
36
#include "Replacer.h"
47
#include "ReplaceEntry.h"
@@ -20,17 +23,67 @@ ReplaceEntry convert(const char *text, Match *match)
2023
return ReplaceEntry(fullMatch->start(), fullMatch->end(), "yyy", 3);
2124
}
2225

26+
27+
void deleteEntry(ReplaceEntry* entry)
28+
{
29+
delete entry;
30+
}
31+
2332
TEST_F(ReplacerTest, SimpleReplace) {
2433
NppPythonScript::Replacer replacer;
25-
std::list<std::shared_ptr<NppPythonScript::ReplaceEntry> > entries;
26-
bool moreEntries = replacer.startReplace("aaabbbaaabbb", 12, "bbb", convert, entries);
34+
std::list<NppPythonScript::ReplaceEntry* > entries;
35+
bool moreEntries = replacer.startReplace("aaabbbaaabb", 12, "(b+)", "x$1x", entries);
2736
ASSERT_EQ(2, entries.size());
28-
std::list<std::shared_ptr<NppPythonScript::ReplaceEntry> >::const_iterator it = entries.begin();
37+
std::list<NppPythonScript::ReplaceEntry*>::const_iterator it = entries.begin();
2938

3039
ASSERT_EQ(3, (*it)->getStart());
40+
ASSERT_STREQ("xbbbx", (*it)->getReplacement());
41+
ASSERT_EQ(5, (*it)->getReplacementLength());
42+
3143
++it;
44+
3245
ASSERT_EQ(9, (*it)->getStart());
46+
ASSERT_STREQ("xbbx", (*it)->getReplacement());
47+
ASSERT_EQ(4, (*it)->getReplacementLength());
3348
ASSERT_EQ(false, moreEntries);
49+
50+
for_each(entries.begin(), entries.end(), deleteEntry);
3451
}
3552

36-
}
53+
54+
/** Test ensures that the replacer is treating the text as UTF8, and not simply a byte sequence
55+
*/
56+
TEST_F(ReplacerTest, ReplaceUtf8) {
57+
NppPythonScript::Replacer replacer;
58+
std::list<NppPythonScript::ReplaceEntry* > entries;
59+
bool moreEntries = replacer.startReplace("aaa\xc3\xb4" "bbbaaa\xc3\xbc" "bb", 15, "aaa([\xc3\xbc])", "x$1x", entries);
60+
ASSERT_EQ(1, entries.size());
61+
std::list<NppPythonScript::ReplaceEntry*>::const_iterator it = entries.begin();
62+
63+
ASSERT_EQ(8, (*it)->getStart());
64+
ASSERT_STREQ("x\xC3\xBCx", (*it)->getReplacement());
65+
ASSERT_EQ(4, (*it)->getReplacementLength());
66+
67+
68+
for_each(entries.begin(), entries.end(), deleteEntry);
69+
}
70+
71+
/** Test ensures characters outside of the BMP are matched correctly
72+
*/
73+
TEST_F(ReplacerTest, ReplaceExtendedUtf8) {
74+
NppPythonScript::Replacer replacer;
75+
std::list<NppPythonScript::ReplaceEntry* > entries;
76+
bool moreEntries = replacer.startReplace("aaa\xF0\x9F\x82\xB7" "ZZZ" "bbbaaa\xF0\x9F\x82\xB8" "ZZZ", 23, "aaa([\xF0\x9F\x82\xB8])", "x$1x", entries);
77+
ASSERT_EQ(1, entries.size());
78+
std::list<NppPythonScript::ReplaceEntry*>::const_iterator it = entries.begin();
79+
80+
ASSERT_EQ(13, (*it)->getStart());
81+
ASSERT_STREQ("x\xF0\x9F\x82\xB8x", (*it)->getReplacement());
82+
ASSERT_EQ(6, (*it)->getReplacementLength());
83+
84+
for_each(entries.begin(), entries.end(), deleteEntry);
85+
}
86+
87+
}
88+
89+

PythonScript/include/PythonScript/NppPythonScript.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
*/
2222
#define PYSCR_SHOWCONSOLE (PYSCR_START+2)
2323

24+
25+
#define PYSCR_INTERNAL_START (PYSCR_START + 100)
26+
27+
/** Internal: Runs a replacement on the current document
28+
* (wParam = 0, ReplacementContainer *rc)
29+
*/
30+
#define PYSCR_RUNREPLACE (PYSCR_INTERNAL_START + 0)
31+
2432
/** Use PYSCRF_SYNC in the flags member to run the script or statement
2533
* synchronously (i.e. within the same thread). The SendMessage() call
2634
* will return when the script or statement has completed.

PythonScript/project/PythonScript2010.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
<ClCompile Include="..\src\PythonConsole.cpp" />
241241
<ClCompile Include="..\src\PythonHandler.cpp" />
242242
<ClCompile Include="..\src\PythonScript.cpp" />
243+
<ClCompile Include="..\src\ReplacementContainer.cpp" />
243244
<ClCompile Include="..\src\Replacer.cpp" />
244245
<ClCompile Include="..\src\ScintillaCells.cpp" />
245246
<ClCompile Include="..\src\ScintillaPython.cpp" />
@@ -283,6 +284,7 @@
283284
<ClInclude Include="..\src\PythonScriptVersion.h" />
284285
<ClInclude Include="..\res\resource.h" />
285286
<ClInclude Include="..\src\ReplaceEntry.h" />
287+
<ClInclude Include="..\src\ReplacementContainer.h" />
286288
<ClInclude Include="..\src\Replacer.h" />
287289
<ClInclude Include="..\src\ScintillaCells.h" />
288290
<ClInclude Include="..\src\ScintillaNotifications.h" />

PythonScript/project/PythonScript2010.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<ClCompile Include="..\src\UtfConversion.cpp">
118118
<Filter>Source Files</Filter>
119119
</ClCompile>
120+
<ClCompile Include="..\src\ReplacementContainer.cpp">
121+
<Filter>Source Files</Filter>
122+
</ClCompile>
120123
</ItemGroup>
121124
<ItemGroup>
122125
<ClInclude Include="..\src\AboutDialog.h">
@@ -233,6 +236,9 @@
233236
<ClInclude Include="..\src\Match.h">
234237
<Filter>Header Files</Filter>
235238
</ClInclude>
239+
<ClInclude Include="..\src\ReplacementContainer.h">
240+
<Filter>Header Files</Filter>
241+
</ClInclude>
236242
</ItemGroup>
237243
<ItemGroup>
238244
<ResourceCompile Include="..\res\PythonScript.rc">

PythonScript/src/Match.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace NppPythonScript
1818
virtual int groupCount() = 0;
1919
virtual GroupDetail* group(int groupNumber) = 0;
2020
virtual GroupDetail* groupName(const char *groupName) = 0;
21+
virtual void expand(const char* format, char **result, int *resultLength) = 0;
2122
};
2223

2324

PythonScript/src/NotepadPlusWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ ScintillaWrapper NotepadPlusWrapper::createScintilla()
375375
LRESULT handle = callNotepad(NPPM_CREATESCINTILLAHANDLE, 0, NULL);
376376

377377
// return copy
378-
return ScintillaWrapper((HWND)handle);
378+
return ScintillaWrapper((HWND)handle, m_nppHandle);
379379
}
380380

381381
void NotepadPlusWrapper::destroyScintilla(ScintillaWrapper& buffer)

PythonScript/src/PythonConsole.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "NotepadPlusWrapper.h"
1515

1616
PythonConsole::PythonConsole(HWND hNotepad) :
17-
mp_scintillaWrapper(new ScintillaWrapper(NULL)),
17+
mp_scintillaWrapper(new ScintillaWrapper(NULL, hNotepad)),
1818
mp_consoleDlg(new ConsoleDialog()),
1919
mp_mainThreadState(NULL),
2020
m_statementRunning(CreateEvent(NULL, FALSE, TRUE, NULL)),

PythonScript/src/PythonHandler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ PythonHandler::PythonHandler(TCHAR *pluginsDir, TCHAR *configDir, HINSTANCE hIns
2828

2929
mp_notepad = createNotepadPlusWrapper();
3030
mp_scintilla = createScintillaWrapper();
31-
mp_scintilla1 = new ScintillaWrapper(scintilla1Handle);
32-
mp_scintilla2 = new ScintillaWrapper(scintilla2Handle);
31+
mp_scintilla1 = new ScintillaWrapper(scintilla1Handle, m_nppHandle);
32+
mp_scintilla2 = new ScintillaWrapper(scintilla2Handle, m_nppHandle);
3333
}
3434

3535
PythonHandler::~PythonHandler(void)
@@ -74,7 +74,7 @@ PythonHandler::~PythonHandler(void)
7474
ScintillaWrapper* PythonHandler::createScintillaWrapper()
7575
{
7676
m_currentView = mp_notepad->getCurrentView();
77-
return new ScintillaWrapper(m_currentView ? m_scintilla2Handle : m_scintilla1Handle);
77+
return new ScintillaWrapper(m_currentView ? m_scintilla2Handle : m_scintilla1Handle, m_nppHandle);
7878
}
7979

8080
NotepadPlusWrapper* PythonHandler::createNotepadPlusWrapper()

0 commit comments

Comments
 (0)