summaryrefslogtreecommitdiff
path: root/support/highlight/src/core/preformatter.h
blob: 4afcb284646e23a3ef79de6bc39afd846ce63b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/***************************************************************************
                        PreFormatter.cpp  -  description
                             -------------------
    begin                : Mo Jan 03 2005
    copyright            : (C) 2005-2008 by Andre Simon
    email                : andre.simon1@gmx.de
 ***************************************************************************/


/*
This file is part of Highlight.

Highlight is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Highlight is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef PreFormatter_H
#define PreFormatter_H

#include <string>
#include <set>

namespace highlight
{

	/** \brief Class which provides intelligent line wrapping.
	* @author Andre Simon
	*/

	class PreFormatter
	{
		public:

			PreFormatter();

			~PreFormatter();

			/**
			 Set wrapping mode
			 \param wrap set to true if long lines should be wrapped
			*/
			void setWrap ( bool wrap ) {wrapLines = wrap;}

			/**
			 Replace tabs by spaces
			 \param replTabs set to true if tabs should be replaced by spaces
			*/
			void setReplaceTabs ( bool replTabs ) {replaceTabs = replTabs;}

			/**
			 \return True if current line can be wrapped again
			*/
			bool hasMoreLines();

			/**
			 Sets new line to be wrapped
			 \param newline New line
			*/
			void setLine ( const std::string & newline );

			/**
			 The method will indent function calls and statements
			 \return Next line
			*/
			std::string getNextLine();

			/**
			 \return True if lines following open braces should be indented
			*/
			bool indentCode();

			/**
			  Maximum line length
			  \param maxlength max. length of output lines
			*/
			void setWrapLineLength ( unsigned int maxlength );

			/**
			  Indentation mode
			  \param indentAfterOpenBraces set true if lines should be indented after braces
			*/
			void setWrapIndentBraces ( bool indentAfterOpenBraces=true );

			/**
			  Number of spaces
			  \param num number of spaces which replace a tab
			*/
			void setNumberSpaces ( unsigned int num );

			/**
			  \return true if preformatting is enabled
			*/
			bool isEnabled()
			{
				return wrapLines || replaceTabs;
			}

			/**
			  reset preformatting state to use the object with new input data
			*/
			void reset ()
			{
				lineNumber=0;
				wrappedLines.clear();
			}

			/**
			 \param lineNumber line number
			  \return true if input line linenumber was wrapped
			*/
			bool isWrappedLine ( int lineNumber )
			{
				return wrappedLines.count ( lineNumber );
			}

		private:

			unsigned int maxLineLength;

			std::string line, wsPrefix;
			unsigned int index;
			unsigned int numberSpaces;
			unsigned int lineNumber;
			size_t wsPrefixLength;
			bool hasMore, indentAfterOpenBraces;
			bool redefineWsPrefix;
			bool wrapLines, replaceTabs;

			std::set<int> wrappedLines;

			static const std::string LB_CHARS;
			static const std::string WS_CHARS;
			static const std::string INDENT_MARKERS;

	};

}

#endif