1 /*
2     SPDX-License-Identifier: GPL-2.0-or-later
3 
4     SPDX-FileCopyrightText: 2003 Brian Thomas <thomas@mail630.gsfc.nasa.gov>
5     SPDX-FileCopyrightText: 2004-2020 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
6 */
7 
8 // own header
9 #include "cppcodedocumentation.h"
10 
11 // app includes
12 #include "codedocument.h"
13 #include "codegenerator.h"
14 #include "codegenerationpolicy.h"
15 #include "uml.h"
16 
17 // qt includes
18 #include <QRegExp>
19 
CPPCodeDocumentation(CodeDocument * doc,const QString & text)20 CPPCodeDocumentation::CPPCodeDocumentation(CodeDocument * doc, const QString & text)
21   : CodeComment(doc, text)
22 {
23 }
24 
~CPPCodeDocumentation()25 CPPCodeDocumentation::~CPPCodeDocumentation()
26 {
27 }
28 
29 /**
30  * Save the XMI representation of this object
31  */
saveToXMI1(QXmlStreamWriter & writer)32 void CPPCodeDocumentation::saveToXMI1(QXmlStreamWriter& writer)
33 {
34     writer.writeStartElement(QLatin1String("cppcodedocumentation"));
35     setAttributesOnNode(writer); // as we added no additional fields to this class we may
36     // just use parent TextBlock method
37     writer.writeEndElement();
38 }
39 
40 /**
41  * @return   QString
42  */
toString() const43 QString CPPCodeDocumentation::toString() const
44 {
45     QString output;
46 
47     // simple output method
48     if(getWriteOutText())
49     {
50         bool useDoubleDashOutput = true;
51 
52         // need to figure out output type from cpp policy
53         CodeGenerationPolicy * p = UMLApp::app()->commonPolicy();
54         if(p->getCommentStyle() == CodeGenerationPolicy::MultiLine)
55             useDoubleDashOutput = false;
56 
57         QString indent = getIndentationString();
58         QString endLine = getNewLineEndingChars();
59         QString body = getText();
60         if(useDoubleDashOutput)
61         {
62             if(!body.isEmpty())
63                 output.append(formatMultiLineText (body, indent + QLatin1String("// "), endLine));
64         } else {
65             output.append(indent + QLatin1String("/**") + endLine);
66             output.append(formatMultiLineText (body, indent + QLatin1String(" * "), endLine));
67             output.append(indent + QLatin1String(" */") + endLine);
68         }
69     }
70 
71     return output;
72 }
73 
getNewEditorLine(int amount)74 QString CPPCodeDocumentation::getNewEditorLine(int amount)
75 {
76     CodeGenerationPolicy * p = UMLApp::app()->commonPolicy();
77     if(p->getCommentStyle() == CodeGenerationPolicy::MultiLine)
78         return getIndentationString(amount) + QLatin1String(" * ");
79     else
80         return getIndentationString(amount) + QLatin1String("// ");
81 }
82 
firstEditableLine()83 int CPPCodeDocumentation::firstEditableLine()
84 {
85     CodeGenerationPolicy * p = UMLApp::app()->commonPolicy();
86     if(p->getCommentStyle() == CodeGenerationPolicy::MultiLine)
87         return 1;
88     return 0;
89 }
90 
lastEditableLine()91 int CPPCodeDocumentation::lastEditableLine()
92 {
93     CodeGenerationPolicy * p = UMLApp::app()->commonPolicy();
94     if(p->getCommentStyle() == CodeGenerationPolicy::MultiLine)
95     {
96         return -1; // very last line is NOT editable
97     }
98     return 0;
99 }
100 
101 /** UnFormat a long text string. Typically, this means removing
102  *  the indentation (linePrefix) and/or newline chars from each line.
103  */
unformatText(const QString & text,const QString & indent)104 QString CPPCodeDocumentation::unformatText(const QString & text, const QString & indent)
105 {
106     QString mytext = TextBlock::unformatText(text, indent);
107     CodeGenerationPolicy * p = UMLApp::app()->commonPolicy();
108     // remove leading or trailing comment stuff
109     mytext.remove(QRegExp(QLatin1Char('^') + indent));
110     if(p->getCommentStyle() == CodeGenerationPolicy::MultiLine)
111     {
112         mytext.remove(QRegExp(QLatin1String("^\\/\\*\\*\\s*\n?")));
113         mytext.remove(QRegExp(QLatin1String("\\s*\\*\\/\\s*\n?$")));
114         mytext.remove(QRegExp(QLatin1String("^\\s*\\*\\s*")));
115     } else
116         mytext.remove(QRegExp(QLatin1String("^\\/\\/\\s*")));
117 
118     return mytext;
119 }
120