[SourceKit] Pass Text around more.

This helps de-couple us from the Swift Editor Document.
This commit is contained in:
John Regner
2016-02-28 10:37:53 -08:00
parent 8f728f31ad
commit 3283a8cc03
2 changed files with 10 additions and 11 deletions

View File

@@ -1470,15 +1470,15 @@ class CodeFormatter {
SwiftEditorDocument &Doc; SwiftEditorDocument &Doc;
EditorConsumer &Consumer; EditorConsumer &Consumer;
public: public:
CodeFormatter(SwiftEditorDocument &Doc, EditorConsumer& Consumer) CodeFormatter(SwiftEditorDocument &Doc, EditorConsumer &Consumer)
:Doc(Doc), Consumer(Consumer) { } :Doc(Doc), Consumer(Consumer) { }
SwiftEditorLineRange indent(unsigned LineIndex, FormatContext &FC) { std::pair<SwiftEditorLineRange,StringRef> indent(unsigned LineIndex, FormatContext &FC, StringRef Text) {
auto &FmtOptions = Doc.getFormatOptions(); auto &FmtOptions = Doc.getFormatOptions();
// If having sibling locs to align with, respect siblings. // If having sibling locs to align with, respect siblings.
if (FC.HasSibling()) { if (FC.HasSibling()) {
StringRef Line = Doc.getTrimmedTextForLine(LineIndex); StringRef Line = Doc.getTrimmedTextForLine(LineIndex, Text);
StringBuilder Builder; StringBuilder Builder;
FC.padToSiblingColumn(Builder); FC.padToSiblingColumn(Builder);
if (FC.needExtraIndentationForSibling()) { if (FC.needExtraIndentationForSibling()) {
@@ -1489,7 +1489,7 @@ public:
} }
Builder.append(Line); Builder.append(Line);
Consumer.recordFormattedText(Builder.str().str()); Consumer.recordFormattedText(Builder.str().str());
return SwiftEditorLineRange(LineIndex, 1); return std::make_pair(SwiftEditorLineRange(LineIndex, 1), Text);
} }
// Take the current indent position of the outer context, then add another // Take the current indent position of the outer context, then add another
@@ -1517,7 +1517,7 @@ public:
} }
// Reformat the specified line with the calculated indent. // Reformat the specified line with the calculated indent.
StringRef Line = Doc.getTrimmedTextForLine(LineIndex); StringRef Line = Doc.getTrimmedTextForLine(LineIndex, Text);
std::string IndentedLine; std::string IndentedLine;
if (FmtOptions.UseTabs) if (FmtOptions.UseTabs)
IndentedLine.assign(ExpandedIndent / FmtOptions.TabWidth, '\t'); IndentedLine.assign(ExpandedIndent / FmtOptions.TabWidth, '\t');
@@ -1528,7 +1528,8 @@ public:
Consumer.recordFormattedText(IndentedLine); Consumer.recordFormattedText(IndentedLine);
// Return affected line range, which can later be more than one line. // Return affected line range, which can later be more than one line.
return SwiftEditorLineRange(LineIndex, 1); SwiftEditorLineRange range = SwiftEditorLineRange(LineIndex, 1);
return std::make_pair(range, Text);
} }
}; };
@@ -1986,8 +1987,7 @@ void SwiftEditorDocument::formatText(unsigned Line, unsigned Length,
SourceLoc Loc = SM.getLocForBufferStart(BufID).getAdvancedLoc(Offset); SourceLoc Loc = SM.getLocForBufferStart(BufID).getAdvancedLoc(Offset);
FormatContext FC = walker.walkToLocation(Loc); FormatContext FC = walker.walkToLocation(Loc);
CodeFormatter CF(*this, Consumer); CodeFormatter CF(*this, Consumer);
SwiftEditorLineRange LineRange = CF.indent(Line, FC); SwiftEditorLineRange LineRange = CF.indent(Line, FC, Text).first;
Consumer.recordAffectedLineRange(LineRange.startLine(), LineRange.lineCount()); Consumer.recordAffectedLineRange(LineRange.startLine(), LineRange.lineCount());
} }
@@ -2151,8 +2151,7 @@ size_t SwiftEditorDocument::getExpandedIndentForLine(unsigned LineIndex) {
return Indent; return Indent;
} }
StringRef SwiftEditorDocument::getTrimmedTextForLine(unsigned LineIndex) { StringRef SwiftEditorDocument::getTrimmedTextForLine(unsigned LineIndex, StringRef Text) {
StringRef Text = Impl.EditableBuffer->getBuffer()->getText();
size_t LineOffset = getOffsetOfTrimmedLine(LineIndex, Text); size_t LineOffset = getOffsetOfTrimmedLine(LineIndex, Text);
size_t LineEnd = Text.find_first_of("\r\n", LineOffset); size_t LineEnd = Text.find_first_of("\r\n", LineOffset);
return Text.slice(LineOffset, LineEnd); return Text.slice(LineOffset, LineEnd);

View File

@@ -88,7 +88,7 @@ public:
void formatText(unsigned Line, unsigned Length, EditorConsumer &Consumer); void formatText(unsigned Line, unsigned Length, EditorConsumer &Consumer);
void expandPlaceholder(unsigned Offset, unsigned Length, void expandPlaceholder(unsigned Offset, unsigned Length,
EditorConsumer &Consumer); EditorConsumer &Consumer);
StringRef getTrimmedTextForLine(unsigned Line); StringRef getTrimmedTextForLine(unsigned Line, StringRef Text);
size_t getExpandedIndentForLine(unsigned LineIndex); size_t getExpandedIndentForLine(unsigned LineIndex);
const swift::ide::CodeFormatOptions &getFormatOptions(); const swift::ide::CodeFormatOptions &getFormatOptions();