Skip to content

Commit 96922e7

Browse files
authored
Merge pull request #80 from kapilratnani/bug-56-line-break
use line ending setting from editor
2 parents 6ad6ea3 + 40b2e66 commit 96922e7

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

NppJSONViewer/PluginDefinition.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,22 @@ void formatSelectedJSON()
245245
bool useTabs = ::SendMessage(curScintilla, SCI_GETUSETABS, 0, 0);
246246
unsigned indentLen = useTabs ? 1 : static_cast<unsigned>(::SendMessage(curScintilla, SCI_GETTABWIDTH, 0, 0));
247247
char indentChar = useTabs ? '\t' : ' ';
248+
249+
LRESULT eolMode = ::SendMessage(curScintilla, SCI_GETEOLMODE, 0, 0);
250+
switch (eolMode) {
251+
case 0:
252+
pw.SetLineEnding(rapidjson::LineEndingOption::kCrLf);
253+
break;
254+
case 1:
255+
pw.SetLineEnding(rapidjson::LineEndingOption::kCr);
256+
break;
257+
default:
258+
pw.SetLineEnding(rapidjson::LineEndingOption::kLf);
259+
break;
260+
}
261+
248262
pw.SetIndent(indentChar, indentLen);
263+
249264
rapidjson::StringStream ss(curJSON);
250265
rapidjson::Reader reader;
251266

NppJSONViewer/rapidjson/prettywriter.h

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ enum PrettyFormatOptions {
3737
kFormatSingleLineArray = 1 //!< Format arrays on a single line.
3838
};
3939

40+
enum LineEndingOption {
41+
kLf = 0, // default line ending \n
42+
kCrLf = 1, // \r\n for windows
43+
kCr = 2 // \r for Mac
44+
};
45+
4046
//! Writer with indentation and spacing.
4147
/*!
4248
\tparam OutputStream Type of ouptut os.
@@ -56,7 +62,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
5662
\param levelDepth Initial capacity of stack.
5763
*/
5864
explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
59-
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {}
65+
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault), lineEndingOption_(kLf) {}
6066

6167

6268
explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
@@ -79,6 +85,11 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
7985
return *this;
8086
}
8187

88+
PrettyWriter& SetLineEnding(LineEndingOption lineEndingOption) {
89+
lineEndingOption_ = lineEndingOption;
90+
return *this;
91+
}
92+
8293
//! Set pretty writer formatting options.
8394
/*! \param options Formatting options.
8495
*/
@@ -143,7 +154,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
143154
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
144155

145156
if (!empty) {
146-
Base::os_->Put('\n');
157+
WriteLineEnding();
147158
WriteIndent();
148159
}
149160
bool ret = Base::WriteEndObject();
@@ -167,7 +178,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
167178
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
168179

169180
if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
170-
Base::os_->Put('\n');
181+
WriteLineEnding();
171182
WriteIndent();
172183
}
173184
bool ret = Base::WriteEndArray();
@@ -218,23 +229,23 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
218229
}
219230

220231
if (!(formatOptions_ & kFormatSingleLineArray)) {
221-
Base::os_->Put('\n');
232+
WriteLineEnding();
222233
WriteIndent();
223234
}
224235
}
225236
else { // in object
226237
if (level->valueCount > 0) {
227238
if (level->valueCount % 2 == 0) {
228239
Base::os_->Put(',');
229-
Base::os_->Put('\n');
240+
WriteLineEnding();
230241
}
231242
else {
232243
Base::os_->Put(':');
233244
Base::os_->Put(' ');
234245
}
235246
}
236247
else
237-
Base::os_->Put('\n');
248+
WriteLineEnding();
238249

239250
if (level->valueCount % 2 == 0)
240251
WriteIndent();
@@ -254,10 +265,25 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
254265
PutN(*Base::os_, static_cast<typename OutputStream::Ch>(indentChar_), count);
255266
}
256267

268+
void WriteLineEnding() {
269+
switch (lineEndingOption_) {
270+
case kCrLf:
271+
Base::os_->Put('\r');
272+
Base::os_->Put('\n');
273+
break;
274+
case kCr:
275+
Base::os_->Put('\r');
276+
break;
277+
default:
278+
Base::os_->Put('\n');
279+
break;
280+
}
281+
}
282+
257283
Ch indentChar_;
258284
unsigned indentCharCount_;
259285
PrettyFormatOptions formatOptions_;
260-
286+
LineEndingOption lineEndingOption_;
261287
private:
262288
// Prohibit copy constructor & assignment operator.
263289
PrettyWriter(const PrettyWriter&);

0 commit comments

Comments
 (0)