Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/util/formattinghelpers.cpp
Show First 20 Lines • Show All 93 Lines • ▼ Show 20 Line(s) | 81 | { | |||
---|---|---|---|---|---|
94 | while(textWhitespaceEnd < text.size() && text[textWhitespaceEnd].isSpace()) | 94 | while(textWhitespaceEnd < text.size() && text[textWhitespaceEnd].isSpace()) | ||
95 | ++textWhitespaceEnd; | 95 | ++textWhitespaceEnd; | ||
96 | 96 | | |||
97 | QString contextWhiteSpace = context.mid(contextPosition); | 97 | QString contextWhiteSpace = context.mid(contextPosition); | ||
98 | contextPosition = 0; | 98 | contextPosition = 0; | ||
99 | QString textWhiteSpace = text.left(textWhitespaceEnd); | 99 | QString textWhiteSpace = text.left(textWhitespaceEnd); | ||
100 | 100 | | |||
101 | // Step 1: Remove redundant newlines | 101 | // Step 1: Remove redundant newlines | ||
102 | while(contextWhiteSpace.contains('\n') && textWhiteSpace.contains('\n')) | 102 | while(contextWhiteSpace.contains(QLatin1Char('\n')) && textWhiteSpace.contains(QLatin1Char('\n'))) | ||
103 | { | 103 | { | ||
104 | int contextOffset = contextWhiteSpace.indexOf('\n')+1; | 104 | int contextOffset = contextWhiteSpace.indexOf(QLatin1Char('\n'))+1; | ||
105 | int textOffset = textWhiteSpace.indexOf('\n')+1; | 105 | int textOffset = textWhiteSpace.indexOf(QLatin1Char('\n'))+1; | ||
106 | 106 | | |||
107 | contextPosition += contextOffset; | 107 | contextPosition += contextOffset; | ||
108 | contextWhiteSpace.remove(0, contextOffset); | 108 | contextWhiteSpace.remove(0, contextOffset); | ||
109 | 109 | | |||
110 | textPosition += textOffset; | 110 | textPosition += textOffset; | ||
111 | textWhiteSpace.remove(0, textOffset); | 111 | textWhiteSpace.remove(0, textOffset); | ||
112 | } | 112 | } | ||
113 | 113 | | |||
114 | int contextOffset = 0; | 114 | int contextOffset = 0; | ||
115 | int textOffset = 0; | 115 | int textOffset = 0; | ||
116 | // Skip redundant ordinary whitespace | 116 | // Skip redundant ordinary whitespace | ||
117 | while( contextOffset < contextWhiteSpace.size() && textOffset < textWhiteSpace.size() && contextWhiteSpace[contextOffset].isSpace() && contextWhiteSpace[contextOffset] != '\n' && textWhiteSpace[textOffset].isSpace() && textWhiteSpace[textOffset] != '\n' ) | 117 | while(contextOffset < contextWhiteSpace.size() && textOffset < textWhiteSpace.size() && contextWhiteSpace[contextOffset].isSpace() && contextWhiteSpace[contextOffset] != QLatin1Char('\n') && textWhiteSpace[textOffset].isSpace() && textWhiteSpace[textOffset] != QLatin1Char('\n')) | ||
118 | { | 118 | { | ||
119 | bool contextWasTab = contextWhiteSpace[contextOffset] == ' '; | 119 | bool contextWasTab = contextWhiteSpace[contextOffset] == QLatin1Char('\t'); | ||
120 | bool textWasTab = textWhiteSpace[contextOffset] == ' '; | 120 | bool textWasTab = textWhiteSpace[contextOffset] == QLatin1Char('\t'); | ||
121 | ++contextOffset; | 121 | ++contextOffset; | ||
122 | ++textOffset; | 122 | ++textOffset; | ||
123 | if( contextWasTab != textWasTab ) | 123 | if( contextWasTab != textWasTab ) | ||
124 | { | 124 | { | ||
125 | // Problem: We have a mismatch of tabs and/or ordinary whitespaces | 125 | // Problem: We have a mismatch of tabs and/or ordinary whitespaces | ||
126 | if( contextWasTab ) | 126 | if( contextWasTab ) | ||
127 | { | 127 | { | ||
128 | for( int s = 1; s < tabWidth; ++s ) | 128 | for( int s = 1; s < tabWidth; ++s ) | ||
129 | if( textOffset < textWhiteSpace.size() && textWhiteSpace[textOffset] == ' ' ) | 129 | if (textOffset < textWhiteSpace.size() && textWhiteSpace[textOffset] == QLatin1Char(' ')) | ||
130 | ++textOffset; | 130 | ++textOffset; | ||
131 | }else if( textWasTab ) | 131 | }else if( textWasTab ) | ||
132 | { | 132 | { | ||
133 | for( int s = 1; s < tabWidth; ++s ) | 133 | for( int s = 1; s < tabWidth; ++s ) | ||
134 | if( contextOffset < contextWhiteSpace.size() && contextWhiteSpace[contextOffset] == ' ' ) | 134 | if (contextOffset < contextWhiteSpace.size() && contextWhiteSpace[contextOffset] == QLatin1Char(' ')) | ||
135 | ++contextOffset; | 135 | ++contextOffset; | ||
136 | } | 136 | } | ||
137 | } | 137 | } | ||
138 | } | 138 | } | ||
139 | 139 | | |||
140 | return textPosition+textOffset; | 140 | return textPosition+textOffset; | ||
141 | } | 141 | } | ||
142 | 142 | | |||
Show All 21 Lines | 147 | if(!leftContext.isEmpty()) { | |||
164 | 164 | | |||
165 | int skip = skipRedundantWhiteSpace( leftContext, formattedMergedText, tabWidth ); | 165 | int skip = skipRedundantWhiteSpace( leftContext, formattedMergedText, tabWidth ); | ||
166 | 166 | | |||
167 | formattedMergedText = formattedMergedText.mid(skip); | 167 | formattedMergedText = formattedMergedText.mid(skip); | ||
168 | } | 168 | } | ||
169 | 169 | | |||
170 | if(!rightContext.isEmpty()) { | 170 | if(!rightContext.isEmpty()) { | ||
171 | //Add a whitespace behind the text for matching, so that we definitely capture all trailing whitespace | 171 | //Add a whitespace behind the text for matching, so that we definitely capture all trailing whitespace | ||
172 | int endOfText = matchPrefixIgnoringWhitespace( formattedMergedText, text+' ', QString() ); | 172 | int endOfText = matchPrefixIgnoringWhitespace(formattedMergedText, text + QLatin1Char(' '), QString()); | ||
173 | if(endOfText == -1) { | 173 | if(endOfText == -1) { | ||
174 | // Try 2: Ignore the fuzzy characters while matching | 174 | // Try 2: Ignore the fuzzy characters while matching | ||
175 | endOfText = matchPrefixIgnoringWhitespace( formattedMergedText, text+' ', fuzzyCharacters ); | 175 | endOfText = matchPrefixIgnoringWhitespace(formattedMergedText, text + QLatin1Char(' '), fuzzyCharacters); | ||
176 | if(endOfText == -1) { | 176 | if(endOfText == -1) { | ||
177 | qCWarning(UTIL) << "problem matching the text while formatting"; | 177 | qCWarning(UTIL) << "problem matching the text while formatting"; | ||
178 | return text; | 178 | return text; | ||
179 | } | 179 | } | ||
180 | } | 180 | } | ||
181 | formattedMergedText = formattedMergedText.left(endOfText); | 181 | formattedMergedText = formattedMergedText.left(endOfText); | ||
182 | 182 | | |||
183 | int skip = skipRedundantWhiteSpace( reverse(rightContext), reverse(formattedMergedText), tabWidth ); | 183 | int skip = skipRedundantWhiteSpace( reverse(rightContext), reverse(formattedMergedText), tabWidth ); | ||
Show All 10 Lines |