Skip to content

Commit

Permalink
Add refactoring tool
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Feb 6, 2016
1 parent f3e3f84 commit 396b9f6
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
8 changes: 8 additions & 0 deletions nazuna/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.o
*.swp
.DS_Store

## Output
bin/
build/
*.xcodeproj
17 changes: 17 additions & 0 deletions nazuna/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
NORI = ../nori/bin/nori
PRODUCTNAME = nazuna
HEADERS = ../somera/*.h
SOURCES = ../somera/*.cpp main.cpp

xcode:
$(NORI) \
-generator=xcode \
-o $(PRODUCTNAME) \
-std=c++14 \
-stdlib=libc++ \
-I.. \
$(HEADERS) \
$(SOURCES)
@xcodebuild -project $(PRODUCTNAME).xcodeproj -configuration Release
@mkdir -p bin
@cp build/Release/$(PRODUCTNAME) bin/$(PRODUCTNAME)
24 changes: 24 additions & 0 deletions nazuna/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# nazuna

A simple C/C++ refactoring tool.

It is distributed under the [MIT License](https://opensource.org/licenses/MIT).

## Features

* Add new line to end of file.
* Replace tabs with white spaces
* Remove trailing spaces, tabs and extra newlines

## Build and run

```sh
# Setup build tool
make -C ../nori bootstrap

# Build
make xcode

# Run
./bin/nazuna -help
```
100 changes: 100 additions & 0 deletions nazuna/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2015 mogemimi. Distributed under the MIT license.

#include "somera/CommandLineParser.h"
#include "somera/FileSystem.h"
#include "somera/Optional.h"
#include "somera/StringHelper.h"
#include <iostream>
#include <fstream>

using somera::CommandLineParser;
using somera::Optional;
using somera::NullOpt;
namespace StringHelper = somera::StringHelper;

namespace {

void setupCommandLineParser(CommandLineParser & parser)
{
using somera::CommandLineArgumentType::Flag;
using somera::CommandLineArgumentType::JoinedOrSeparate;
parser.setUsageText("norichan [options ...] [build file ...]");
parser.addArgument("-h", Flag, "Display available options");
parser.addArgument("-help", Flag, "Display available options");
}

std::string removeUnnecessaryWhitespace(const std::string& text)
{
using StringHelper::trimRight;
std::string result;
for (auto & line : StringHelper::split(text, '\n')) {
result += trimRight(trimRight(line, ' '), '\t');
result += '\n';
}
return std::move(result);
}

std::string replaceHardTabsWithWhiteSpaces(const std::string& text)
{
constexpr auto spaces = " ";
return StringHelper::replace(text, "\t", spaces);
}

std::string replaceCRLFWithLF(const std::string& text)
{
return StringHelper::replace(text, "\r\n", "\n");
}

std::string trimLastLineBreaks(const std::string& text)
{
return StringHelper::trimRight(text, '\n') + '\n';
}

void refactorSourceCode(const std::string& path)
{
std::ifstream input(path);
if (!input) {
return;
}
std::istreambuf_iterator<char> start(input);
std::istreambuf_iterator<char> end;
std::string text(start, end);
input.close();

std::ofstream output(path, std::ios::out | std::ios::trunc);
if (!output) {
return;
}
text = replaceCRLFWithLF(text);
text = replaceHardTabsWithWhiteSpaces(text);
text = removeUnnecessaryWhitespace(text);
text = trimLastLineBreaks(text);
output << text;
}

} // unnamed namespace

int main(int argc, char *argv[])
{
CommandLineParser parser;
setupCommandLineParser(parser);
parser.parse(argc, argv);

if (parser.hasParseError()) {
std::cerr << parser.getErrorMessage() << std::endl;
return 1;
}
if (parser.exists("-h") || parser.exists("-help")) {
std::cout << parser.getHelpText() << std::endl;
return 0;
}
if (parser.getPaths().empty()) {
std::cerr << "error: no input file" << std::endl;
return 1;
}

for (auto & path : parser.getPaths()) {
refactorSourceCode(path);
}
return 0;
}

0 comments on commit 396b9f6

Please sign in to comment.