mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Move FormatContext & FormatWalker into new file. Move CodeFormatOptions out of SwiftEditorDocument. Move functions getTrimmedLineOffset & getLineOffset out of SwiftEditor Document. Rename Offset of Line functions Declaring that the get the offset of some line index is more clear.
46 lines
1.6 KiB
C++
46 lines
1.6 KiB
C++
//===--- FormatContext.cpp --------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "FormatContext.h"
|
|
|
|
size_t swift::ide::getOffsetOfLine(unsigned LineIndex, StringRef Text) {
|
|
// SourceLoc start = SourceLoc(llvm::SMLoc::getFromPointer(Text.begin()));
|
|
// FIXME: We should have a cached line map in EditableTextBuffer, for now
|
|
// we just do the slow naive thing here.
|
|
size_t LineOffset = 0;
|
|
unsigned CurrentLine = 0;
|
|
while (LineOffset < Text.size() && ++CurrentLine < LineIndex) {
|
|
LineOffset = Text.find_first_of("\r\n", LineOffset);
|
|
if (LineOffset != std::string::npos) {
|
|
++LineOffset;
|
|
if (LineOffset < Text.size() &&
|
|
Text[LineOffset - 1] == '\r' && Text[LineOffset] == '\n')
|
|
++LineOffset;
|
|
}
|
|
|
|
}
|
|
if (LineOffset == std::string::npos)
|
|
LineOffset = 0;
|
|
return LineOffset;
|
|
}
|
|
|
|
size_t swift::ide::getOffsetOfTrimmedLine(unsigned LineIndex, StringRef Text) {
|
|
size_t LineOffset = swift::ide::getOffsetOfLine(LineIndex, Text);
|
|
|
|
// Skip leading whitespace.
|
|
size_t FirstNonWSOnLine = Text.find_first_not_of(" \t\v\f", LineOffset);
|
|
if (FirstNonWSOnLine != std::string::npos)
|
|
LineOffset = FirstNonWSOnLine;
|
|
|
|
return LineOffset;
|
|
}
|