Skip to content

[SourceKit] Share common object printer code #3163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//===--- RequestResponsePrinterBase.h - -------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SOURCEKITD_REQUESTRESPONSEPRINTERBASE_H
#define LLVM_SOURCEKITD_REQUESTRESPONSEPRINTERBASE_H

#include "sourcekitd/sourcekitd.h"
#include "sourcekitd/Logging.h"
#include <vector>

namespace llvm {
class StringRef;
template <typename T> class ArrayRef;
class raw_ostream;
}
namespace SourceKit {
class UIdent;
}

namespace sourcekitd {

template <typename VisitorImplClass, typename VisitedType>
class RequestResponsePrinterBase {
llvm::raw_ostream &OS;
unsigned Indent;
bool PrintAsJSON;
public:
typedef std::vector<std::pair<SourceKit::UIdent, VisitedType>> DictMap;

RequestResponsePrinterBase(llvm::raw_ostream &OS, unsigned Indent = 0,
bool PrintAsJSON = false)
: OS(OS), Indent(Indent), PrintAsJSON(PrintAsJSON) { }

void visitNull() {
OS << "<<NULL>>";
}

void visitDictionary(const DictMap &Map) {
OS << "{\n";
Indent += 2;
for (unsigned i = 0, e = Map.size(); i != e; ++i) {
auto &Pair = Map[i];
OS.indent(Indent);
if (PrintAsJSON) {
visitString(Pair.first.getName());
} else {
OSColor(OS, DictKeyColor) << Pair.first.getName();
}
OS << ": ";
static_cast<VisitorImplClass *>(this)->visit(Pair.second);
if (i < e-1)
OS << ',';
OS << '\n';
}
Indent -= 2;
OS.indent(Indent) << '}';
}

void visitArray(llvm::ArrayRef<VisitedType> Arr) {
OS << "[\n";
Indent += 2;
for (unsigned i = 0, e = Arr.size(); i != e; ++i) {
auto Obj = Arr[i];
OS.indent(Indent);
static_cast<VisitorImplClass *>(this)->visit(Obj);
if (i < e-1)
OS << ',';
OS << '\n';
}
Indent -= 2;
OS.indent(Indent) << ']';
}

void visitInt64(int64_t Val) {
OS << Val;
}

void visitBool(bool Val) {
OS << Val;
}

void visitString(llvm::StringRef Str) {
OS << '\"';
// Avoid raw_ostream's write_escaped, we don't want to escape unicode
// characters because it will be invalid JSON.
writeEscaped(Str, OS);
OS << '\"';
}

void visitUID(llvm::StringRef UID) {
if (PrintAsJSON) {
visitString(UID);
} else {
OSColor(OS, UIDColor) << UID;
}
}
};

}

#endif
74 changes: 5 additions & 69 deletions tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "DictionaryKeys.h"
#include "sourcekitd/Internal.h"
#include "sourcekitd/Logging.h"
#include "sourcekitd/RequestResponsePrinterBase.h"
#include "SourceKit/Support/Logging.h"
#include "SourceKit/Support/UIdent.h"
#include "llvm/ADT/ArrayRef.h"
Expand Down Expand Up @@ -298,77 +299,12 @@ class VariantVisitor {
}
};

class VariantPrinter : public VariantVisitor<VariantPrinter> {
raw_ostream &OS;
unsigned Indent;
bool PrintAsJSON;
class VariantPrinter : public VariantVisitor<VariantPrinter>,
public RequestResponsePrinterBase<VariantPrinter,
sourcekitd_variant_t> {
public:
VariantPrinter(raw_ostream &OS, unsigned Indent = 0, bool PrintAsJSON = false)
: OS(OS), Indent(Indent), PrintAsJSON(PrintAsJSON) { }

void visitNull() {
OS << "<<NULL>>";
}

void visitDictionary(const DictMap &Map) {
OS << "{\n";
Indent += 2;
for (unsigned i = 0, e = Map.size(); i != e; ++i) {
auto &Pair = Map[i];
OS.indent(Indent);
if (PrintAsJSON) {
visitString(Pair.first.getName());
} else {
OSColor(OS, DictKeyColor) << Pair.first.getName();
}
OS << ": ";
VariantPrinter(OS, Indent, PrintAsJSON).visit(Pair.second);
if (i < e-1)
OS << ',';
OS << '\n';
}
Indent -= 2;
OS.indent(Indent) << '}';
}

void visitArray(ArrayRef<sourcekitd_variant_t> Arr) {
OS << "[\n";
Indent += 2;
for (unsigned i = 0, e = Arr.size(); i != e; ++i) {
auto Obj = Arr[i];
OS.indent(Indent);
VariantPrinter(OS, Indent, PrintAsJSON).visit(Obj);
if (i < e-1)
OS << ',';
OS << '\n';
}
Indent -= 2;
OS.indent(Indent) << ']';
}

void visitInt64(int64_t Val) {
OS << Val;
}

void visitBool(bool Val) {
OS << Val;
}

void visitString(StringRef Str) {
OS << '\"';
// Avoid raw_ostream's write_escaped, we don't want to escape unicode
// characters because it will be invalid JSON.
writeEscaped(Str, OS);
OS << '\"';
}

void visitUID(StringRef UID) {
if (PrintAsJSON) {
visitString(UID);
} else {
OSColor(OS, UIDColor) << UID;
}
}
: RequestResponsePrinterBase(OS, Indent, PrintAsJSON) { }
};
}

Expand Down
59 changes: 5 additions & 54 deletions tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-XPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
#include "sourcekitd/CodeCompletionResultsArray.h"
#include "sourcekitd/DocSupportAnnotationArray.h"
#include "sourcekitd/TokenAnnotationsArray.h"
#include "sourcekitd/Logging.h"
#include "sourcekitd/RequestResponsePrinterBase.h"
#include "SourceKit/Support/UIdent.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include <map>
#include <vector>
#include <xpc/xpc.h>

Expand Down Expand Up @@ -137,60 +136,12 @@ class SKDObjectVisitor {
}
};

class SKDObjectPrinter : public SKDObjectVisitor<SKDObjectPrinter> {
raw_ostream &OS;
unsigned Indent;
class SKDObjectPrinter : public SKDObjectVisitor<SKDObjectPrinter>,
public RequestResponsePrinterBase<SKDObjectPrinter,
sourcekitd_object_t> {
public:
SKDObjectPrinter(raw_ostream &OS, unsigned Indent = 0)
: OS(OS), Indent(Indent) { }

void visitDictionary(const DictMap &Map) {
OS << "{\n";
Indent += 2;
for (unsigned i = 0, e = Map.size(); i != e; ++i) {
auto &Pair = Map[i];
OS.indent(Indent);
OSColor(OS, DictKeyColor) << Pair.first.getName();
OS << ": ";
SKDObjectPrinter(OS, Indent).visit(Pair.second);
if (i < e-1)
OS << ',';
OS << '\n';
}
Indent -= 2;
OS.indent(Indent) << '}';
}

void visitArray(ArrayRef<sourcekitd_object_t> Arr) {
OS << "[\n";
Indent += 2;
for (unsigned i = 0, e = Arr.size(); i != e; ++i) {
auto Obj = Arr[i];
OS.indent(Indent);
SKDObjectPrinter(OS, Indent).visit(Obj);
if (i < e-1)
OS << ',';
OS << '\n';
}
Indent -= 2;
OS.indent(Indent) << ']';
}

void visitInt64(int64_t Val) {
OS << Val;
}

void visitString(StringRef Str) {
OS << '\"';
// Avoid raw_ostream's write_escaped, we don't want to escape unicode
// characters because it will be invalid JSON.
writeEscaped(Str, OS);
OS << '\"';
}

void visitUID(StringRef UID) {
OSColor(OS, UIDColor) << UID;
}
: RequestResponsePrinterBase(OS, Indent) { }
};

} // anonymous namespace.
Expand Down