Skip to content

Commit 8c56ce8

Browse files
authored
Merge pull request swiftlang#33632 from benlangmuir/gardening-libIDE
[gardening] Move some code between sourcekitd and libIDE
2 parents ee8ad7b + c5b42ac commit 8c56ce8

15 files changed

+413
-380
lines changed

include/swift/IDE/CodeCompletionResultPrinter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ void printCodeCompletionResultTypeName(
3434
void printCodeCompletionResultTypeNameAnnotated(
3535
const CodeCompletionResult &Result, llvm::raw_ostream &OS);
3636

37+
void printCodeCompletionResultSourceText(
38+
const CodeCompletionResult &Result, llvm::raw_ostream &OS);
39+
40+
void printCodeCompletionResultFilterName(
41+
const CodeCompletionResult &Result, llvm::raw_ostream &OS);
42+
3743
} // namespace ide
3844
} // namespace swift
3945

tools/SourceKit/include/SourceKit/Support/FuzzyStringMatcher.h renamed to include/swift/IDE/FuzzyStringMatcher.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef LLVM_SOURCEKIT_LIB_SUPPORT_FUZZYSTRINGMATCHER_H
14-
#define LLVM_SOURCEKIT_LIB_SUPPORT_FUZZYSTRINGMATCHER_H
13+
#ifndef SWIFT_IDE_FUZZYSTRINGMATCHER_H
14+
#define SWIFT_IDE_FUZZYSTRINGMATCHER_H
1515

16-
#include "SourceKit/Core/LLVM.h"
16+
#include "swift/Basic/LLVM.h"
1717
#include "llvm/ADT/BitVector.h"
1818
#include <string>
1919

20-
namespace SourceKit {
20+
namespace swift {
21+
namespace ide {
2122

2223
/// FuzzyStringMatcher compares candidate strings against a pattern
2324
/// string using a fuzzy matching algorithm and provides a numerical
@@ -49,6 +50,7 @@ class FuzzyStringMatcher {
4950
double scoreCandidate(StringRef candidate) const;
5051
};
5152

52-
} // end namespace SourceKit
53+
} // namespace ide
54+
} // namespace swift
5355

54-
#endif // LLVM_SOURCEKIT_LIB_SUPPORT_FUZZYSTRINGMATCHER_H
56+
#endif // SWIFT_IDE_FUZZYSTRINGMATCHER_H

include/swift/IDE/Utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ SourceCompleteResult
8181
isSourceInputComplete(std::unique_ptr<llvm::MemoryBuffer> MemBuf, SourceFileKind SFKind);
8282
SourceCompleteResult isSourceInputComplete(StringRef Text, SourceFileKind SFKind);
8383

84+
bool initCompilerInvocation(
85+
CompilerInvocation &Invocation, ArrayRef<const char *> OrigArgs,
86+
DiagnosticEngine &Diags, StringRef UnresolvedPrimaryFile,
87+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
88+
const std::string &runtimeResourcePath,
89+
const std::string &diagnosticDocumentationPath,
90+
bool shouldOptimizeForIDE, time_t sessionTimestamp, std::string &Error);
91+
8492
bool initInvocationByClangArguments(ArrayRef<const char *> ArgList,
8593
CompilerInvocation &Invok,
8694
std::string &Error);

lib/IDE/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_swift_host_library(swiftIDE STATIC
77
ConformingMethodList.cpp
88
ExprContextAnalysis.cpp
99
Formatting.cpp
10+
FuzzyStringMatcher.cpp
1011
Refactoring.cpp
1112
ModuleInterfacePrinting.cpp
1213
REPLCodeCompletion.cpp
@@ -21,6 +22,7 @@ add_swift_host_library(swiftIDE STATIC
2122
target_link_libraries(swiftIDE PRIVATE
2223
swiftAST
2324
swiftClangImporter
25+
swiftDriver
2426
swiftFrontend
2527
swiftIndex
2628
swiftParse

lib/IDE/CodeCompletionResultPrinter.cpp

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "swift/Basic/LLVM.h"
1413
#include "swift/IDE/CodeCompletionResultPrinter.h"
14+
#include "swift/AST/ASTPrinter.h"
15+
#include "swift/Basic/LLVM.h"
1516
#include "swift/IDE/CodeCompletion.h"
1617
#include "swift/Markup/XMLUtils.h"
1718
#include "llvm/Support/raw_ostream.h"
@@ -255,3 +256,186 @@ void swift::ide::printCodeCompletionResultTypeNameAnnotated(const CodeCompletion
255256
AnnotatingResultPrinter printer(OS);
256257
printer.printTypeName(Result);
257258
}
259+
260+
/// Provide the text for the call parameter, including constructing a typed
261+
/// editor placeholder for it.
262+
static void
263+
constructTextForCallParam(ArrayRef<CodeCompletionString::Chunk> ParamGroup,
264+
raw_ostream &OS) {
265+
assert(ParamGroup.front().is(ChunkKind::CallParameterBegin));
266+
267+
for (; !ParamGroup.empty(); ParamGroup = ParamGroup.slice(1)) {
268+
auto &C = ParamGroup.front();
269+
if (C.isAnnotation())
270+
continue;
271+
if (C.is(ChunkKind::CallParameterInternalName) ||
272+
C.is(ChunkKind::CallParameterType) ||
273+
C.is(ChunkKind::CallParameterTypeBegin) ||
274+
C.is(ChunkKind::CallParameterClosureExpr)) {
275+
break;
276+
}
277+
if (!C.hasText())
278+
continue;
279+
OS << C.getText();
280+
}
281+
282+
SmallString<32> DisplayString;
283+
SmallString<32> TypeString;
284+
SmallString<32> ExpansionTypeString;
285+
286+
for (auto i = ParamGroup.begin(), e = ParamGroup.end(); i != e; ++i) {
287+
auto &C = *i;
288+
if (C.is(ChunkKind::CallParameterTypeBegin)) {
289+
assert(TypeString.empty());
290+
auto nestingLevel = C.getNestingLevel();
291+
++i;
292+
for (; i != e; ++i) {
293+
if (i->endsPreviousNestedGroup(nestingLevel))
294+
break;
295+
if (!i->isAnnotation() && i->hasText()) {
296+
TypeString += i->getText();
297+
DisplayString += i->getText();
298+
}
299+
}
300+
--i;
301+
continue;
302+
}
303+
if (C.is(ChunkKind::CallParameterClosureType)) {
304+
assert(ExpansionTypeString.empty());
305+
ExpansionTypeString = C.getText();
306+
continue;
307+
}
308+
if (C.is(ChunkKind::CallParameterType)) {
309+
assert(TypeString.empty());
310+
TypeString = C.getText();
311+
}
312+
if (C.is(ChunkKind::CallParameterClosureExpr)) {
313+
// We have a closure expression, so provide it directly instead of in
314+
// a placeholder.
315+
OS << "{";
316+
if (!C.getText().empty())
317+
OS << " " << C.getText();
318+
OS << "\n" << getCodePlaceholder() << "\n}";
319+
return;
320+
}
321+
if (C.isAnnotation() || !C.hasText())
322+
continue;
323+
DisplayString += C.getText();
324+
}
325+
326+
StringRef Display = DisplayString.str();
327+
StringRef Type = TypeString.str();
328+
StringRef ExpansionType = ExpansionTypeString.str();
329+
if (ExpansionType.empty())
330+
ExpansionType = Type;
331+
332+
OS << "<#T##" << Display;
333+
if (Display == Type && Display == ExpansionType) {
334+
// Short version, display and type are the same.
335+
} else {
336+
OS << "##" << Type;
337+
if (ExpansionType != Type)
338+
OS << "##" << ExpansionType;
339+
}
340+
OS << "#>";
341+
}
342+
343+
void swift::ide::printCodeCompletionResultSourceText(
344+
const CodeCompletionResult &Result, llvm::raw_ostream &OS) {
345+
auto Chunks = Result.getCompletionString()->getChunks();
346+
for (size_t i = 0; i < Chunks.size(); ++i) {
347+
auto &C = Chunks[i];
348+
if (C.is(ChunkKind::BraceStmtWithCursor)) {
349+
OS << " {\n" << getCodePlaceholder() << "\n}";
350+
continue;
351+
}
352+
if (C.is(ChunkKind::CallParameterBegin)) {
353+
size_t Start = i++;
354+
for (; i < Chunks.size(); ++i) {
355+
if (Chunks[i].endsPreviousNestedGroup(C.getNestingLevel()))
356+
break;
357+
}
358+
constructTextForCallParam(Chunks.slice(Start, i - Start), OS);
359+
--i;
360+
continue;
361+
}
362+
if (C.is(ChunkKind::TypeAnnotationBegin)) {
363+
// Skip type annotation structure.
364+
auto level = C.getNestingLevel();
365+
do {
366+
++i;
367+
} while (i != Chunks.size() && !Chunks[i].endsPreviousNestedGroup(level));
368+
--i;
369+
}
370+
if (!C.isAnnotation() && C.hasText()) {
371+
OS << C.getText();
372+
}
373+
}
374+
}
375+
376+
void swift::ide::printCodeCompletionResultFilterName(
377+
const CodeCompletionResult &Result, llvm::raw_ostream &OS) {
378+
auto str = Result.getCompletionString();
379+
// FIXME: we need a more uniform way to handle operator completions.
380+
if (str->getChunks().size() == 1 && str->getChunks()[0].is(ChunkKind::Dot)) {
381+
OS << ".";
382+
return;
383+
} else if (str->getChunks().size() == 2 &&
384+
str->getChunks()[0].is(ChunkKind::QuestionMark) &&
385+
str->getChunks()[1].is(ChunkKind::Dot)) {
386+
OS << "?.";
387+
return;
388+
}
389+
390+
auto FirstTextChunk = str->getFirstTextChunkIndex();
391+
if (FirstTextChunk.hasValue()) {
392+
auto chunks = str->getChunks().slice(*FirstTextChunk);
393+
for (auto i = chunks.begin(), e = chunks.end(); i != e; ++i) {
394+
auto &C = *i;
395+
396+
if (C.is(ChunkKind::BraceStmtWithCursor))
397+
break; // Don't include brace-stmt in filter name.
398+
399+
if (C.is(ChunkKind::Equal)) {
400+
OS << C.getText();
401+
break;
402+
}
403+
404+
bool shouldPrint = !C.isAnnotation();
405+
switch (C.getKind()) {
406+
case ChunkKind::TypeAnnotation:
407+
case ChunkKind::CallParameterInternalName:
408+
case ChunkKind::CallParameterClosureType:
409+
case ChunkKind::CallParameterClosureExpr:
410+
case ChunkKind::CallParameterType:
411+
case ChunkKind::DeclAttrParamColon:
412+
case ChunkKind::Comma:
413+
case ChunkKind::Whitespace:
414+
case ChunkKind::Ellipsis:
415+
case ChunkKind::Ampersand:
416+
case ChunkKind::OptionalMethodCallTail:
417+
continue;
418+
case ChunkKind::CallParameterTypeBegin:
419+
case ChunkKind::TypeAnnotationBegin: {
420+
// Skip call parameter type or type annotation structure.
421+
auto nestingLevel = C.getNestingLevel();
422+
do {
423+
++i;
424+
} while (i != e && !i->endsPreviousNestedGroup(nestingLevel));
425+
--i;
426+
continue;
427+
}
428+
case ChunkKind::CallParameterColon:
429+
// Since we don't add the type, also don't add the space after ':'.
430+
if (shouldPrint)
431+
OS << ":";
432+
continue;
433+
default:
434+
break;
435+
}
436+
437+
if (C.hasText() && shouldPrint)
438+
OS << C.getText();
439+
}
440+
}
441+
}

tools/SourceKit/lib/Support/FuzzyStringMatcher.cpp renamed to lib/IDE/FuzzyStringMatcher.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "SourceKit/Support/FuzzyStringMatcher.h"
13+
#include "swift/IDE/FuzzyStringMatcher.h"
1414
#include "clang/Basic/CharInfo.h"
1515
#include "llvm/ADT/ArrayRef.h"
1616
#include "llvm/ADT/SmallString.h"
1717

18-
using namespace SourceKit;
18+
using namespace swift;
19+
using namespace swift::ide;
1920
using clang::toUppercase;
2021
using clang::toLowercase;
2122
using clang::isUppercase;

0 commit comments

Comments
 (0)