MCPcopy Create free account
hub / github.com/KDE/kdevelop / skipRedundantWhiteSpace

Function skipRedundantWhiteSpace

kdevplatform/util/formattinghelpers.cpp:962–1021  ·  view source on GitHub ↗

Returns the text start position with all whitespace that is redundant in the given context skipped

Source from the content-addressed store, hash-verified

960
961// Returns the text start position with all whitespace that is redundant in the given context skipped
962int skipRedundantWhiteSpace(QStringView context, QStringView text, int tabWidth)
963{
964 if (context.isEmpty() || !context[context.size() - 1].isSpace() || text.isEmpty() || !text[0].isSpace())
965 return 0;
966
967 int textPosition = 0;
968
969 // Extract trailing whitespace in the context
970 int contextPosition = context.size() - 1;
971 while (contextPosition > 0 && context[contextPosition - 1].isSpace())
972 --contextPosition;
973
974 int textWhitespaceEnd = 0;
975 while (textWhitespaceEnd < text.size() && text[textWhitespaceEnd].isSpace())
976 ++textWhitespaceEnd;
977
978 auto contextWhiteSpace = context.sliced(contextPosition);
979 auto textWhiteSpace = text.first(textWhitespaceEnd);
980
981 // Step 1: Remove redundant newlines
982 while (contextWhiteSpace.contains(QLatin1Char('\n')) && textWhiteSpace.contains(QLatin1Char('\n'))) {
983 int contextOffset = contextWhiteSpace.indexOf(QLatin1Char('\n')) + 1;
984 int textOffset = textWhiteSpace.indexOf(QLatin1Char('\n')) + 1;
985
986 contextWhiteSpace = contextWhiteSpace.sliced(contextOffset);
987
988 textPosition += textOffset;
989 textWhiteSpace = textWhiteSpace.sliced(textOffset);
990 }
991
992 int contextOffset = 0;
993 int textOffset = 0;
994 // Skip redundant ordinary whitespace
995 while (contextOffset < contextWhiteSpace.size() && textOffset < textWhiteSpace.size() &&
996 contextWhiteSpace[contextOffset].isSpace() && contextWhiteSpace[contextOffset] != QLatin1Char('\n') &&
997 textWhiteSpace[textOffset].isSpace() && textWhiteSpace[textOffset] != QLatin1Char('\n')) {
998 bool contextWasTab = contextWhiteSpace[contextOffset] == QLatin1Char('\t');
999 bool textWasTab = textWhiteSpace[contextOffset] == QLatin1Char('\t');
1000 ++contextOffset;
1001 ++textOffset;
1002 if (contextWasTab != textWasTab) {
1003 // Problem: We have a mismatch of tabs and/or ordinary whitespaces
1004 if (contextWasTab) {
1005 for (int s = 1; s < tabWidth; ++s)
1006 if (textOffset < textWhiteSpace.size() && textWhiteSpace[textOffset] == QLatin1Char(' '))
1007 ++textOffset;
1008 } else if (textWasTab) {
1009 for (int s = 1; s < tabWidth; ++s)
1010 if (contextOffset < contextWhiteSpace.size() &&
1011 contextWhiteSpace[contextOffset] == QLatin1Char(' '))
1012 ++contextOffset;
1013 }
1014 }
1015 }
1016 textPosition += textOffset;
1017
1018 Q_ASSERT(textPosition >= 0);
1019 Q_ASSERT(textPosition <= text.size());

Callers 1

Calls 5

isEmptyMethod · 0.45
sizeMethod · 0.45
firstMethod · 0.45
containsMethod · 0.45
indexOfMethod · 0.45

Tested by

no test coverage detected