Skip to content

Commit

Permalink
Bug 770660 - Code snippet always shows line numbers from 1
Browse files Browse the repository at this point in the history
This patch makes the handling of the \snippet and other commands consistent between the different languages (no line numbers anymore with python) and also introduces analogous to \includelineno  the command \snippetlineno.

Some non relevant changes:
- *code.l Calculation of the end line was incorrect, in case of a snippet the end line was the number of lines of the snippet and not reltive to the start line.
- *code.l made consistent over the different laguages, enabling exBlock and inlineFragment
- testing/indexpage.xml in test 14 the \snippet command was used with python and giving line numbers, linenumbers are now gone (consistency)
  • Loading branch information
albert-github authored and Dimitri van Heesch committed Sep 3, 2016
1 parent 4453475 commit 9ae1af9
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 43 deletions.
19 changes: 14 additions & 5 deletions doc/commands.doc
Expand Up @@ -179,6 +179,7 @@ documentation:
\refitem cmdskipline \\skipline
\refitem cmdsnippet \\snippet
\refitem cmdsnippetdoc \\snippetdoc
\refitem cmdsnippetlineno \\snippetlineno
\refitem cmdstartuml \\startuml
\refitem cmdstruct \\struct
\refitem cmdsubpage \\subpage
Expand Down Expand Up @@ -2183,7 +2184,7 @@ Commands for displaying examples
This command works the same way as \ref cmdinclude "\\include", but will add line
numbers to the included file.

\sa section \ref cmdinclude "\\include".
\sa sections \ref cmdinclude "\\include" and \ref cmdsnippetlineno "\\snippetlineno".

<hr>
\section cmdincludedoc \\includedoc <file-name>
Expand Down Expand Up @@ -2303,7 +2304,16 @@ Commands for displaying examples
see section \ref cmddontinclude "\\dontinclude" for an alternative way
to include fragments of a source file that does not require markers.

\sa section \ref cmdsnippetdoc "\\snippetdoc".
\sa section \ref cmdsnippetdoc "\\snippetdoc" and \ref cmdsnippetlineno "\\snippetlineno".
<hr>
\section cmdsnippetlineno \\snippetlineno <file-name> ( block_id )

\addindex \\snippetlineno
This command works the same way as \ref cmdsnippet "\\snippet", but will add line
numbers to the included snippet.

\sa sections \ref cmdsnippet "\\snippet" and \ref cmdincludelineno "\\includelineno".

<hr>
\section cmdsnippetdoc \\snippetdoc <file-name> ( block_id )

Expand Down Expand Up @@ -3212,9 +3222,8 @@ class Receiver
\c \\verbatim command or the parser will get confused!

\sa sections \ref cmdcode "\\code",
\ref cmdendverbatim "\\endverbatim",
\ref cmdverbinclude "\\verbinclude", and
\ref cmdverbincludedooc "\\verbincludedooc".
\ref cmdendverbatim "\\endverbatim" and
\ref cmdverbinclude "\\verbinclude".

<hr>
\section cmdxmlonly \\xmlonly
Expand Down
1 change: 1 addition & 0 deletions src/cmdmapper.cpp
Expand Up @@ -89,6 +89,7 @@ CommandMap cmdMap[] =
{ "section", CMD_SECTION },
{ "snippet", CMD_SNIPPET },
{ "snippetdoc", CMD_SNIPPETDOC },
{ "snippetlineno", CMD_SNIPWITHLINES },
{ "subpage", CMD_SUBPAGE },
{ "subsection", CMD_SUBSECTION },
{ "subsubsection", CMD_SUBSUBSECTION },
Expand Down
3 changes: 2 additions & 1 deletion src/cmdmapper.h
Expand Up @@ -135,7 +135,8 @@ enum CommandType
CMD_PLUS = 105,
CMD_MINUS = 106,
CMD_INCLUDEDOC = 107,
CMD_SNIPPETDOC = 108
CMD_SNIPPETDOC = 108,
CMD_SNIPWITHLINES= 109
};

enum HtmlTagType
Expand Down
9 changes: 5 additions & 4 deletions src/code.l
Expand Up @@ -3659,16 +3659,17 @@ void parseCCode(CodeOutputInterface &od,const char *className,const QCString &s,
g_searchCtx = searchCtx;
g_collectXRefs = collectXRefs;
g_inFunctionTryBlock = FALSE;
if (endLine!=-1)
g_inputLines = endLine+1;
else
g_inputLines = countLines();

if (startLine!=-1)
g_yyLineNr = startLine;
else
g_yyLineNr = 1;

if (endLine!=-1)
g_inputLines = endLine+1;
else
g_inputLines = g_yyLineNr + countLines() - 1;

g_curlyCount = 0;
g_bodyCurlyCount = 0;
g_bracketCount = 0;
Expand Down
22 changes: 22 additions & 0 deletions src/docbookvisitor.cpp
Expand Up @@ -359,6 +359,28 @@ void DocbookDocVisitor::visit(DocInclude *inc)
);
m_t << "</computeroutput></literallayout>";
break;
case DocInclude::SnipWithLines:
{
QFileInfo cfi( inc->file() );
FileDef fd( cfi.dirPath().utf8(), cfi.fileName().utf8() );
m_t << "<literallayout><computeroutput>";
Doxygen::parserManager->getParser(inc->extension())
->parseCode(m_ci,
inc->context(),
extractBlock(inc->text(),inc->blockId()),
langExt,
inc->isExample(),
inc->exampleFile(),
&fd,
lineBlock(inc->text(),inc->blockId()),
-1, // endLine
FALSE, // inlineFragment
0, // memberDef
TRUE // show line number
);
m_t << "</computeroutput></literallayout>";
}
break;
case DocInclude::SnippetDoc:
case DocInclude::IncludeDoc:
err("Internal inconsistency: found switch SnippetDoc / IncludeDoc in file: %s"
Expand Down
6 changes: 5 additions & 1 deletion src/docparser.cpp
Expand Up @@ -1931,6 +1931,7 @@ void DocInclude::parse()
readTextFileByName(m_file,m_text);
break;
case Snippet:
case SnipWithLines:
readTextFileByName(m_file,m_text);
// check here for the existence of the blockId inside the file, so we
// only generate the warning once.
Expand Down Expand Up @@ -5174,7 +5175,7 @@ void DocPara::handleInclude(const QCString &cmdName,DocInclude::Type t)
}
QCString fileName = g_token->name;
QCString blockId;
if (t==DocInclude::Snippet || t==DocInclude::SnippetDoc)
if (t==DocInclude::Snippet || t==DocInclude::SnipWithLines || t==DocInclude::SnippetDoc)
{
if (fileName == "this") fileName=g_fileName;
doctokenizerYYsetStateSnippet();
Expand Down Expand Up @@ -5701,6 +5702,9 @@ int DocPara::handleCommand(const QCString &cmdName)
case CMD_SNIPPET:
handleInclude(cmdName,DocInclude::Snippet);
break;
case CMD_SNIPWITHLINES:
handleInclude(cmdName,DocInclude::SnipWithLines);
break;
case CMD_INCLUDEDOC:
handleInclude(cmdName,DocInclude::IncludeDoc);
break;
Expand Down
3 changes: 2 additions & 1 deletion src/docparser.h
Expand Up @@ -483,7 +483,8 @@ class DocVerbatim : public DocNode
class DocInclude : public DocNode
{
public:
enum Type { Include, DontInclude, VerbInclude, HtmlInclude, LatexInclude, IncWithLines, Snippet , IncludeDoc, SnippetDoc};
enum Type { Include, DontInclude, VerbInclude, HtmlInclude, LatexInclude,
IncWithLines, Snippet , IncludeDoc, SnippetDoc, SnipWithLines};
DocInclude(DocNode *parent,const QCString &file,
const QCString context, Type t,
bool isExample,const QCString exampleFile,
Expand Down
11 changes: 6 additions & 5 deletions src/fortrancode.l
Expand Up @@ -1263,16 +1263,17 @@ void parseFortranCode(CodeOutputInterface &od,const char *className,const QCStri
g_needsTermination = FALSE;
g_searchCtx = searchCtx;
g_collectXRefs = collectXRefs;
if (endLine!=-1)
g_inputLines = endLine+1;
else
g_inputLines = countLines();

if (startLine!=-1)
g_yyLineNr = startLine;
else
g_yyLineNr = 1;

if (endLine!=-1)
g_inputLines = endLine+1;
else
g_inputLines = g_yyLineNr + countLines() - 1;


g_exampleBlock = exBlock;
g_exampleName = exName;
g_sourceFileDef = fd;
Expand Down
25 changes: 25 additions & 0 deletions src/htmldocvisitor.cpp
Expand Up @@ -578,6 +578,31 @@ void HtmlDocVisitor::visit(DocInclude *inc)
-1, // endLine
TRUE, // inlineFragment
0, // memberDef
FALSE, // show line number
m_ctx // search context
);
m_t << PREFRAG_END;
forceStartParagraph(inc);
}
break;
case DocInclude::SnipWithLines:
{
forceEndParagraph(inc);
m_t << PREFRAG_START;
QFileInfo cfi( inc->file() );
FileDef fd( cfi.dirPath().utf8(), cfi.fileName().utf8() );
Doxygen::parserManager->getParser(inc->extension())
->parseCode(m_ci,
inc->context(),
extractBlock(inc->text(),inc->blockId()),
langExt,
inc->isExample(),
inc->exampleFile(),
&fd,
lineBlock(inc->text(),inc->blockId()),
-1, // endLine
FALSE, // inlineFragment
0, // memberDef
TRUE, // show line number
m_ctx // search context
);
Expand Down
22 changes: 22 additions & 0 deletions src/latexdocvisitor.cpp
Expand Up @@ -483,6 +483,28 @@ void LatexDocVisitor::visit(DocInclude *inc)
m_t << "\\end{DoxyCodeInclude}" << endl;
}
break;
case DocInclude::SnipWithLines:
{
QFileInfo cfi( inc->file() );
FileDef fd( cfi.dirPath().utf8(), cfi.fileName().utf8() );
m_t << "\n\\begin{DoxyCodeInclude}\n";
Doxygen::parserManager->getParser(inc->extension())
->parseCode(m_ci,
inc->context(),
extractBlock(inc->text(),inc->blockId()),
langExt,
inc->isExample(),
inc->exampleFile(),
&fd,
lineBlock(inc->text(),inc->blockId()),
-1, // endLine
FALSE, // inlineFragment
0, // memberDef
TRUE // show line number
);
m_t << "\\end{DoxyCodeInclude}" << endl;
}
break;
case DocInclude::SnippetDoc:
case DocInclude::IncludeDoc:
err("Internal inconsistency: found switch SnippetDoc / IncludeDoc in file: %s"
Expand Down
27 changes: 27 additions & 0 deletions src/mandocvisitor.cpp
Expand Up @@ -306,6 +306,33 @@ void ManDocVisitor::visit(DocInclude *inc)
m_t << ".PP" << endl;
m_firstCol=TRUE;
break;
case DocInclude::SnipWithLines:
{
if (!m_firstCol) m_t << endl;
m_t << ".PP" << endl;
m_t << ".nf" << endl;
QFileInfo cfi( inc->file() );
FileDef fd( cfi.dirPath().utf8(), cfi.fileName().utf8() );
Doxygen::parserManager->getParser(inc->extension())
->parseCode(m_ci,
inc->context(),
extractBlock(inc->text(),inc->blockId()),
langExt,
inc->isExample(),
inc->exampleFile(),
&fd,
lineBlock(inc->text(),inc->blockId()),
-1, // endLine
FALSE, // inlineFragment
0, // memberDef
TRUE // show line number
);
if (!m_firstCol) m_t << endl;
m_t << ".fi" << endl;
m_t << ".PP" << endl;
m_firstCol=TRUE;
}
break;
case DocInclude::SnippetDoc:
case DocInclude::IncludeDoc:
err("Internal inconsistency: found switch SnippetDoc / IncludeDoc in file: %s"
Expand Down
1 change: 1 addition & 0 deletions src/perlmodgen.cpp
Expand Up @@ -716,6 +716,7 @@ void PerlModDocVisitor::visit(DocInclude *inc)
case DocInclude::LatexInclude: type = "latexonly"; break;
case DocInclude::VerbInclude: type = "preformatted"; break;
case DocInclude::Snippet: return;
case DocInclude::SnipWithLines: return;
case DocInclude::SnippetDoc:
case DocInclude::IncludeDoc:
err("Internal inconsistency: found switch SnippetDoc / IncludeDoc in file: %s"
Expand Down
1 change: 1 addition & 0 deletions src/printdocvisitor.h
Expand Up @@ -171,6 +171,7 @@ class PrintDocVisitor : public DocVisitor
case DocInclude::LatexInclude: printf("latexinclude"); break;
case DocInclude::VerbInclude: printf("verbinclude"); break;
case DocInclude::Snippet: printf("snippet"); break;
case DocInclude::SnipWithLines: printf("snipwithlines"); break;
case DocInclude::SnippetDoc:
case DocInclude::IncludeDoc:
err("Internal inconsistency: found switch SnippetDoc / IncludeDoc in file: %s"
Expand Down
17 changes: 9 additions & 8 deletions src/pycode.l
Expand Up @@ -1534,8 +1534,8 @@ static void adjustScopesAndSuites(unsigned indentLength)

void parsePythonCode(CodeOutputInterface &od,const char * /*className*/,
const QCString &s,bool exBlock, const char *exName,
FileDef *fd,int startLine,int endLine,bool /*inlineFragment*/,
MemberDef *,bool,Definition *searchCtx,bool collectXRefs)
FileDef *fd,int startLine,int endLine,bool inlineFragment,
MemberDef *,bool,Definition *searchCtx,bool collectXRefs)
{

//printf("***parseCode()\n");
Expand All @@ -1551,22 +1551,22 @@ void parsePythonCode(CodeOutputInterface &od,const char * /*className*/,
g_needsTermination = FALSE;
g_searchCtx=searchCtx;
g_collectXRefs=collectXRefs;
if (endLine!=-1)
g_inputLines = endLine+1;
else
g_inputLines = countLines();

if (startLine!=-1)
g_yyLineNr = startLine;
else
g_yyLineNr = 1;
if (endLine!=-1)
g_inputLines = endLine+1;
else
g_inputLines = g_yyLineNr + countLines() - 1;


g_exampleBlock = exBlock;
g_exampleName = exName;
g_sourceFileDef = fd;

bool cleanupSourceDef = FALSE;
if (fd==0)
if (exBlock && fd==0)
{
// create a dummy filedef for the example
g_sourceFileDef = new FileDef("",(exName?exName:"generated"));
Expand All @@ -1577,6 +1577,7 @@ void parsePythonCode(CodeOutputInterface &od,const char * /*className*/,
setCurrentDoc("l00001");
}

g_includeCodeFragment = inlineFragment;
// Starts line 1 on the output
startCodeLine();

Expand Down
24 changes: 24 additions & 0 deletions src/rtfdocvisitor.cpp
Expand Up @@ -444,6 +444,30 @@ void RTFDocVisitor::visit(DocInclude *inc)
);
m_t << "}";
break;
case DocInclude::SnipWithLines:
{
QFileInfo cfi( inc->file() );
FileDef fd( cfi.dirPath().utf8(), cfi.fileName().utf8() );
m_t << "{" << endl;
if (!m_lastIsPara) m_t << "\\par" << endl;
m_t << rtf_Style_Reset << getStyle("CodeExample");
Doxygen::parserManager->getParser(inc->extension())
->parseCode(m_ci,
inc->context(),
extractBlock(inc->text(),inc->blockId()),
langExt,
inc->isExample(),
inc->exampleFile(),
&fd,
lineBlock(inc->text(),inc->blockId()),
-1, // endLine
FALSE, // inlineFragment
0, // memberDef
TRUE // show line number
);
m_t << "}";
}
break;
case DocInclude::SnippetDoc:
case DocInclude::IncludeDoc:
err("Internal inconsistency: found switch SnippetDoc / IncludeDoc in file: %s"
Expand Down

0 comments on commit 9ae1af9

Please sign in to comment.