Skip to content

Commit 491b63a

Browse files
committed
Merge branch 'albert-github-feature/bug_snippet_trimleft'
2 parents 8fc9154 + 25d6a9d commit 491b63a

16 files changed

+137
-136
lines changed

doc/commands.doc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,9 +2635,11 @@ Commands for displaying examples
26352635
Note also that the [block_id] markers should appear exactly twice in the
26362636
source file.
26372637

2638-
The `option` can either be `lineno` or `doc`.
2639-
The `option` `lineno` can be used to enable line numbers for the included code if desired.
2640-
The `option` `doc` can be used to treat the file as documentation rather than code.
2638+
- The `option` can either be `lineno`, `trimleft` or `doc`.
2639+
- The `option` `lineno` can be used to enable line numbers for the included code if desired.
2640+
- The `option` `trimleft` can be used to remove the common spacing in front of all lines
2641+
(also taking in account the setting of the \ref cfg_tab_size "TAB_SIZE" tag).
2642+
- The `option` `doc` can be used to treat the file as documentation rather than code.
26412643

26422644
\note When using the `{doc}` option,
26432645
some commands like \ref cmdcond "\\cond" and \ref cmdif "\\if" don't work with

src/clangparser.cpp

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -45,56 +45,6 @@ static std::mutex g_docCrossReferenceMutex;
4545

4646
enum class DetectedLang { Cpp, ObjC, ObjCpp };
4747

48-
static QCString detab(const QCString &s)
49-
{
50-
int tabSize = Config_getInt(TAB_SIZE);
51-
GrowBuf out;
52-
int size = s.length();
53-
const char *data = s.data();
54-
int i=0;
55-
int col=0;
56-
const int maxIndent=1000000; // value representing infinity
57-
int minIndent=maxIndent;
58-
while (i<size)
59-
{
60-
char c = data[i++];
61-
switch(c)
62-
{
63-
case '\t': // expand tab
64-
{
65-
int stop = tabSize - (col%tabSize);
66-
//printf("expand at %d stop=%d\n",col,stop);
67-
col+=stop;
68-
while (stop--) out.addChar(' ');
69-
}
70-
break;
71-
case '\n': // reset column counter
72-
out.addChar(c);
73-
col=0;
74-
break;
75-
case ' ': // increment column counter
76-
out.addChar(c);
77-
col++;
78-
break;
79-
default: // non-whitespace => update minIndent
80-
{
81-
int bytes = getUTF8CharNumBytes(c);
82-
for (int j=0;j<bytes-1 && c!=0; j++)
83-
{
84-
out.addChar(c);
85-
c = data[i++];
86-
}
87-
out.addChar(c);
88-
}
89-
if (col<minIndent) minIndent=col;
90-
col++;
91-
}
92-
}
93-
out.addChar(0);
94-
//printf("detab refIndent=%d\n",refIndent);
95-
return out.get();
96-
}
97-
9848
static const char * keywordToType(const char *keyword)
9949
{
10050
static const StringUnorderedSet flowKeywords({
@@ -275,7 +225,8 @@ void ClangTUParser::parse()
275225
p->numFiles = numUnsavedFiles;
276226
p->sources.resize(numUnsavedFiles);
277227
p->ufs.resize(numUnsavedFiles);
278-
p->sources[0] = detab(fileToString(fileName,filterSourceFiles,TRUE));
228+
int refIndent = 0;
229+
p->sources[0] = detab(fileToString(fileName,filterSourceFiles,TRUE),refIndent);
279230
p->ufs[0].Filename = qstrdup(fileName.data());
280231
p->ufs[0].Contents = p->sources[0].data();
281232
p->ufs[0].Length = p->sources[0].length();
@@ -286,7 +237,7 @@ void ClangTUParser::parse()
286237
++it, i++)
287238
{
288239
p->fileMapping.insert({it->c_str(),static_cast<uint32_t>(i)});
289-
p->sources[i] = detab(fileToString(it->c_str(),filterSourceFiles,TRUE));
240+
p->sources[i] = detab(fileToString(it->c_str(),filterSourceFiles,TRUE),refIndent);
290241
p->ufs[i].Filename = qstrdup(it->c_str());
291242
p->ufs[i].Contents = p->sources[i].data();
292243
p->ufs[i].Length = p->sources[i].length();

src/docbookvisitor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,11 @@ DB_VIS_C
482482
m_t << "</literallayout>";
483483
break;
484484
case DocInclude::Snippet:
485+
case DocInclude::SnippetTrimLeft:
485486
m_t << "<literallayout><computeroutput>";
486487
getCodeParser(inc.extension()).parseCode(m_ci,
487488
inc.context(),
488-
extractBlock(inc.text(),inc.blockId()),
489+
extractBlock(inc.text(),inc.blockId(),inc.type()==DocInclude::SnippetTrimLeft),
489490
langExt,
490491
inc.isExample(),
491492
inc.exampleFile()

src/docnode.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ void DocInclude::parse(DocNodeVariant *)
281281
parser()->readTextFileByName(m_file,m_text);
282282
break;
283283
case Snippet:
284+
case SnippetTrimLeft:
284285
case SnipWithLines:
285286
parser()->readTextFileByName(m_file,m_text);
286287
// check here for the existence of the blockId inside the file, so we
@@ -3526,6 +3527,10 @@ void DocPara::handleInclude(DocNodeVariant *thisVariant,const QCString &cmdName,
35263527
{
35273528
t = DocInclude::SnippetDoc;
35283529
}
3530+
else if (t==DocInclude::Snippet && contains("trimleft"))
3531+
{
3532+
t = DocInclude::SnippetTrimLeft;
3533+
}
35293534
tok=parser()->tokenizer.lex();
35303535
if (tok!=TK_WHITESPACE)
35313536
{
@@ -3565,7 +3570,7 @@ void DocPara::handleInclude(DocNodeVariant *thisVariant,const QCString &cmdName,
35653570
}
35663571
QCString fileName = parser()->context.token->name;
35673572
QCString blockId;
3568-
if (t==DocInclude::Snippet || t==DocInclude::SnipWithLines || t==DocInclude::SnippetDoc)
3573+
if (t==DocInclude::Snippet || t==DocInclude::SnipWithLines || t==DocInclude::SnippetDoc || t == DocInclude::SnippetTrimLeft)
35693574
{
35703575
if (fileName == "this") fileName=parser()->context.fileName;
35713576
parser()->tokenizer.setStateSnippet();

src/docnode.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ class DocInclude : public DocNode
412412
public:
413413
enum Type { Include, DontInclude, VerbInclude, HtmlInclude, LatexInclude,
414414
IncWithLines, Snippet , IncludeDoc, SnippetDoc, SnipWithLines,
415-
DontIncWithLines, RtfInclude, ManInclude, DocbookInclude, XmlInclude};
415+
DontIncWithLines, RtfInclude, ManInclude, DocbookInclude, XmlInclude,
416+
SnippetTrimLeft};
416417
DocInclude(DocParser *parser,DocNodeVariant *parent,const QCString &file,
417418
const QCString &context, Type t,
418419
bool isExample,const QCString &exampleFile,

src/htmldocvisitor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,13 @@ void HtmlDocVisitor::operator()(const DocInclude &inc)
764764
forceStartParagraph(inc);
765765
break;
766766
case DocInclude::Snippet:
767+
case DocInclude::SnippetTrimLeft:
767768
{
768769
forceEndParagraph(inc);
769770
m_ci.startCodeFragment("DoxyCode");
770771
getCodeParser(inc.extension()).parseCode(m_ci,
771772
inc.context(),
772-
extractBlock(inc.text(),inc.blockId()),
773+
extractBlock(inc.text(),inc.blockId(),inc.type()==DocInclude::SnippetTrimLeft),
773774
langExt,
774775
inc.isExample(),
775776
inc.exampleFile(),

src/latexdocvisitor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,12 @@ void LatexDocVisitor::operator()(const DocInclude &inc)
564564
m_t << "\\end{DoxyVerbInclude}\n";
565565
break;
566566
case DocInclude::Snippet:
567+
case DocInclude::SnippetTrimLeft:
567568
{
568569
m_ci.startCodeFragment("DoxyCodeInclude");
569570
getCodeParser(inc.extension()).parseCode(m_ci,
570571
inc.context(),
571-
extractBlock(inc.text(),inc.blockId()),
572+
extractBlock(inc.text(),inc.blockId(),inc.type()==DocInclude::SnippetTrimLeft),
572573
langExt,
573574
inc.isExample(),
574575
inc.exampleFile()

src/mandocvisitor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,13 @@ void ManDocVisitor::operator()(const DocInclude &inc)
328328
m_firstCol=TRUE;
329329
break;
330330
case DocInclude::Snippet:
331+
case DocInclude::SnippetTrimLeft:
331332
if (!m_firstCol) m_t << "\n";
332333
m_t << ".PP\n";
333334
m_t << ".nf\n";
334335
getCodeParser(inc.extension()).parseCode(m_ci,
335336
inc.context(),
336-
extractBlock(inc.text(),inc.blockId()),
337+
extractBlock(inc.text(),inc.blockId(),inc.type()==DocInclude::SnippetTrimLeft),
337338
langExt,
338339
inc.isExample(),
339340
inc.exampleFile()

src/markdown.cpp

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ enum Alignment { AlignNone, AlignLeft, AlignCenter, AlignRight };
120120

121121
//---------- constants -------
122122
//
123-
const char *g_utf8_nbsp = "\xc2\xa0"; // UTF-8 nbsp
124-
const char *g_doxy_nbsp = "&_doxy_nbsp;"; // doxygen escape command for UTF-8 nbsp
123+
const char *g_utf8_nbsp = "\xc2\xa0"; // UTF-8 nbsp
124+
const char *g_doxy_nbsp = "&_doxy_nbsp;"; // doxygen escape command for UTF-8 nbsp
125125
const int codeBlockIndent = 4;
126126

127127
//---------- helpers -------
@@ -3194,73 +3194,6 @@ QCString Markdown::extractPageTitle(QCString &docs,QCString &id, int &prepend)
31943194
return title;
31953195
}
31963196

3197-
QCString Markdown::detab(const QCString &s,int &refIndent)
3198-
{
3199-
AUTO_TRACE("s='{}'",Trace::trunc(s));
3200-
int tabSize = Config_getInt(TAB_SIZE);
3201-
int size = s.length();
3202-
m_out.clear();
3203-
m_out.reserve(size);
3204-
const char *data = s.data();
3205-
int i=0;
3206-
int col=0;
3207-
const int maxIndent=1000000; // value representing infinity
3208-
int minIndent=maxIndent;
3209-
while (i<size)
3210-
{
3211-
char c = data[i++];
3212-
switch(c)
3213-
{
3214-
case '\t': // expand tab
3215-
{
3216-
int stop = tabSize - (col%tabSize);
3217-
//printf("expand at %d stop=%d\n",col,stop);
3218-
col+=stop;
3219-
while (stop--) m_out.addChar(' ');
3220-
}
3221-
break;
3222-
case '\n': // reset column counter
3223-
m_out.addChar(c);
3224-
col=0;
3225-
break;
3226-
case ' ': // increment column counter
3227-
m_out.addChar(c);
3228-
col++;
3229-
break;
3230-
default: // non-whitespace => update minIndent
3231-
if (c<0 && i<size) // multibyte sequence
3232-
{
3233-
// special handling of the UTF-8 nbsp character 0xC2 0xA0
3234-
int nb = isUTF8NonBreakableSpace(data);
3235-
if (nb>0)
3236-
{
3237-
m_out.addStr(g_doxy_nbsp);
3238-
i+=nb-1;
3239-
}
3240-
else
3241-
{
3242-
int bytes = getUTF8CharNumBytes(c);
3243-
for (int j=0;j<bytes-1 && c;j++)
3244-
{
3245-
m_out.addChar(c);
3246-
c = data[i++];
3247-
}
3248-
m_out.addChar(c);
3249-
}
3250-
}
3251-
else
3252-
{
3253-
m_out.addChar(c);
3254-
}
3255-
if (col<minIndent) minIndent=col;
3256-
col++;
3257-
}
3258-
}
3259-
if (minIndent!=maxIndent) refIndent=minIndent; else refIndent=0;
3260-
m_out.addChar(0);
3261-
AUTO_TRACE_EXIT("refIndent={}",refIndent);
3262-
return m_out.get();
3263-
}
32643197

32653198
//---------------------------------------------------------------------------
32663199

src/markdown.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class Markdown
3838
void setIndentLevel(int level) { m_indentLevel = level; }
3939

4040
private:
41-
QCString detab(const QCString &s,int &refIndent);
4241
QCString processQuotations(const QCString &s,int refIndent);
4342
QCString processBlocks(const QCString &s,int indent);
4443
QCString isBlockCommand(const char *data,int offset,int size);

0 commit comments

Comments
 (0)