Skip to content

Commit

Permalink
Changing std::string to std::ostream in handleRequest()
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Feb 17, 2013
1 parent 716e8ad commit 7b2f8ec
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 156 deletions.
3 changes: 2 additions & 1 deletion server/IPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef IRONY_MODE_SERVER_IPLUGIN_H_
#define IRONY_MODE_SERVER_IPLUGIN_H_

#include <ostream>
#include <string>

#include "TUManager.h"
Expand Down Expand Up @@ -48,7 +49,7 @@ class IPlugin
* \return The answer "type" to the request.
*/
virtual std::string handleRequest(const JSONObjectWrapper & data,
std::string & buf) = 0;
std::ostream & out) = 0;

virtual ~IPlugin();
};
Expand Down
27 changes: 11 additions & 16 deletions server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <cerrno>

#include "util/arraysize.hpp"
#include "str/wstring_to_string.h"

// list of plugins
#include "plugins/CodeCompletion.h"
Expand All @@ -32,8 +31,6 @@
namespace {
/// An empty line with EOT
const std::string eot_str = "\nEOT\n";
/// Initial size of the completion buffer
const std::size_t SEXP_BUF_INITIAL_SIZE = 16384;

const std::map<std::string, IPlugin *> generateBundlePlugins(TUManager & tuManager)
{
Expand Down Expand Up @@ -129,24 +126,22 @@ void Server::handleRequest(const std::string & request,
const JSONObjectWrapper & json,
const JSONObjectWrapper & data)
{
std::string intro("(");
std::string type("nil");
std::string buf;
bool hasBuffer = true;
const std::string & buffer = json.check(L"buffer", hasBuffer);

if (hasBuffer) {
intro.append(":buffer \"").append(buffer).append("\" ");
}
std::cout << "(";

if (IPlugin *current = plugins_[request]) {
// Try to minimise the cost of buf += / buf.append() done by the
// handleRequest call.
buf.reserve(SEXP_BUF_INITIAL_SIZE);
if (hasBuffer)
{
std::cout << ":buffer \"" << buffer << "\" ";
}

// fill \c buf with the request answer.
type = current->handleRequest(data, buf);
}
if (IPlugin *current = plugins_[request])
{
// Print request answer to std::cout.
type = current->handleRequest(data, std::cout);
}

std::cout << intro << ":type " << type << " " << buf << ")\n;;EOT" << std::endl;
std::cout << " :type " << type << ")\n;;EOT" << std::endl;
}
2 changes: 0 additions & 2 deletions server/TUManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#include "TUManager.h"

#include "str/wstring_to_string.h"

typedef TUManager::SettingsID SettingsID;
typedef TUManager::Settings Settings;

Expand Down
8 changes: 4 additions & 4 deletions server/plugins/CacheInvalidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ CacheInvalidation::~CacheInvalidation()
{ }

std::string CacheInvalidation::handleRequest(const JSONObjectWrapper & data,
std::string & buf)
std::ostream & out)
{
bool valid = true;
const std::string & file = data.check(L"file", valid);

buf += ":value ";
out << ":value ";
if (! valid)
{
buf += ":error";
out << ":error";
std::clog << "invalid reload-flags request." << std::endl;
} else {
tuManager_.invalidateCachedTU(file);
buf += ":success";
out << ":success";
}
return ":status-code";
}
2 changes: 1 addition & 1 deletion server/plugins/CacheInvalidation.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CacheInvalidation : public IPlugin
* \sa IPlugin
*/
virtual std::string handleRequest(const JSONObjectWrapper & data,
std::string & buf);
std::ostream & out);
};

#endif /* !CACHEINVALIDATION_H_ */
76 changes: 34 additions & 42 deletions server/plugins/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <cstddef>
#include <string>

#include "str/to_string.hpp"
#include "util/arraysize.hpp"

#include "ClangString.h"
Expand All @@ -39,15 +38,15 @@ CodeCompletion::~CodeCompletion()
}

std::string CodeCompletion::handleRequest(const JSONObjectWrapper & data,
std::string & buf)
std::ostream & out)
{
bool valid = true;
const std::string & file = data.check(L"file", valid);
unsigned line = data.check(L"line", valid);
unsigned column = data.check(L"column", valid);
const std::vector<std::string> & flags = data.get(L"flags");

buf += ":results (";
out << ":results (";

if (! valid)
{
Expand All @@ -58,18 +57,18 @@ std::string CodeCompletion::handleRequest(const JSONObjectWrapper & data,
{
// TODO: enhance ? actually the function return false on error,
// we can display the error to the user maybe ?
complete(tu, file, line, column, buf);
complete(tu, file, line, column, out);
}

buf += ")";
out << ")";
return (detailedCompletions_ ? ":completion" : ":completion-simple");
}

bool CodeCompletion::complete(CXTranslationUnit & tu,
const std::string & filename,
unsigned line,
unsigned column,
std::string & buf)
std::ostream & out)
{
CXCodeCompleteResults *completionResults
= clang_codeCompleteAt(tu,
Expand Down Expand Up @@ -105,12 +104,12 @@ bool CodeCompletion::complete(CXTranslationUnit & tu,
{
CXCompletionResult result = completionResults->Results[i];

buf += "\n";
buf += "(";
formatCompletionCursorKind(result.CursorKind, buf);
buf += " . ";
formatCompletionString(result.CompletionString, buf);
buf += ")";
out << "\n";
out << "(";
formatCompletionCursorKind(result.CursorKind, out);
out << " . ";
formatCompletionString(result.CompletionString, out);
out << ")";
}

clang_disposeCodeCompleteResults(completionResults);
Expand Down Expand Up @@ -229,37 +228,30 @@ const char *completionCursorKindIdentifier(CXCursorKind cursorKind)

} // anonymous namespace

/**
* Format a cursor kind to a keyword symbol.
*
* \param cursorKind
* \param buf The string where the keyword symbol should be
* added.
*/
void CodeCompletion::formatCompletionCursorKind(CXCursorKind cursorKind,
std::string & buf)
void CodeCompletion::formatCompletionCursorKind(CXCursorKind cursorKind,
std::ostream & out)
{
buf += completionCursorKindIdentifier(cursorKind);
out << completionCursorKindIdentifier(cursorKind);
}

void CodeCompletion::appendConsCellResult(const std::string & keyword,
const std::string & value,
std::string & buf)
std::ostream & out)
{
buf.append("(").append(keyword).append(" . ").append(value).append(")");
out << "(" << keyword << " . " << value << ")";
}

void CodeCompletion::formatCompletionString(CXCompletionString & completionString,
std::string & buf)
std::ostream & out)
{
bool hasOptional = false;

buf += "(:priority ";
buf += str::to_string<unsigned>(clang_getCompletionPriority(completionString));
buf += " :result ";
out << "(:priority ";
out << clang_getCompletionPriority(completionString);
out << " :result ";

if (detailedCompletions_)
buf += "(";
out << "(";

for (unsigned i = 0, max = clang_getNumCompletionChunks(completionString);
i < max;
Expand All @@ -274,10 +266,10 @@ void CodeCompletion::formatCompletionString(CXCompletionString & completionStrin

CXString text = clang_getCompletionChunkText(completionString, i);

buf += "\"";
out << "\"";
if (const char *ctext = clang_getCString(text))
buf += ctext;
buf += "\"";
out << ctext;
out << "\"";
clang_disposeString(text);
break ; // skip the when found
}
Expand All @@ -288,12 +280,12 @@ void CodeCompletion::formatCompletionString(CXCompletionString & completionStrin
ClangString::Escape);

if (! text.isNull()) {
appendConsCellResult(keyword, text.asString(), buf);
appendConsCellResult(keyword, text.asString(), out);
}
continue ; // kind was a 'chunk text'
}

if (tryFormattingKeywordSymbol(chunkKind, buf))
if (tryFormattingKeywordSymbol(chunkKind, out))
continue ; // kind was convertible to a keyword symbol

if (chunkKind == CXCompletionChunk_Optional) // optional, recursive call
Expand All @@ -302,20 +294,20 @@ void CodeCompletion::formatCompletionString(CXCompletionString & completionStrin
clang_getCompletionChunkCompletionString(completionString, i);

hasOptional = true;
buf += "(:optional . \n\t";
formatCompletionString(optionalString, buf);
buf += ")";
out << "(:optional . \n\t";
formatCompletionString(optionalString, out);
out << ")";
}
}

if (detailedCompletions_)
{
buf += ")"; // close ":result ("
out << ")"; // close ":result ("
if (hasOptional)
buf += " :optional t";
out << " :optional t";
}

buf += ")"; // close "(:priority"
out << ")"; // close "(:priority"
}

namespace {
Expand Down Expand Up @@ -344,12 +336,12 @@ static const struct kindsToLispForm_t // arraysize() can't work with
}

bool CodeCompletion::tryFormattingKeywordSymbol(CXCompletionChunkKind kind,
std::string & buf)
std::ostream & out)
{
for (std::size_t i = 0; i < arraysize(kindsToLispForm); ++i)
if (kindsToLispForm[i].kind == kind)
{
appendConsCellResult(":symbol", kindsToLispForm[i].lispForm, buf);
appendConsCellResult(":symbol", kindsToLispForm[i].lispForm, out);
return true;
}
return false;
Expand Down
Loading

0 comments on commit 7b2f8ec

Please sign in to comment.