Fix #125
This commit is contained in:
parent
bf84542b1f
commit
2f7d42087c
22
README.md
22
README.md
|
@ -47,6 +47,28 @@ dfmt --inplace --space_after_cast=false --max_line_length=80 \
|
||||||
--soft_max_line_length=70 --brace_style=otbs file.d
|
--soft_max_line_length=70 --brace_style=otbs file.d
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Disabling formatting
|
||||||
|
Formatting can be temporarily disabled by placing the comments ```// dfmt off```
|
||||||
|
and ```// dfmt on``` around code that you do not want formatted.
|
||||||
|
|
||||||
|
```d
|
||||||
|
void main(string[] args)
|
||||||
|
{
|
||||||
|
bool optionOne, optionTwo, optionThree;
|
||||||
|
|
||||||
|
// dfmt has no way of knowing that "getopt" is special, so it wraps the
|
||||||
|
// argument list normally
|
||||||
|
getopt(args, "optionOne", &optionOne, "optionTwo", &optionTwo, "optionThree", &optionThree);
|
||||||
|
|
||||||
|
// dfmt off
|
||||||
|
getopt(args,
|
||||||
|
"optionOne", &optionOne,
|
||||||
|
"optionTwo", &optionTwo,
|
||||||
|
"optionThree", &optionThree);
|
||||||
|
// dfmt on
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
**dfmt** uses [EditorConfig](http://editorconfig.org/) configuration files.
|
**dfmt** uses [EditorConfig](http://editorconfig.org/) configuration files.
|
||||||
**dfmt**-specific properties are prefixed with *dfmt_*.
|
**dfmt**-specific properties are prefixed with *dfmt_*.
|
||||||
|
|
2
dub.json
2
dub.json
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "dfmt",
|
"name": "dfmt",
|
||||||
"description": "Dfmt is a formatter for D source code",
|
"description": "Dfmt is a formatter for D source code",
|
||||||
"version": "0.3.4",
|
"version": "0.4.0-alpha",
|
||||||
"targetType": "executable",
|
"targetType": "executable",
|
||||||
"license": "BSL-1.0",
|
"license": "BSL-1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -32,7 +32,7 @@ void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output,
|
||||||
astInformation.cleanup();
|
astInformation.cleanup();
|
||||||
auto tokens = byToken(buffer, config, &cache).array();
|
auto tokens = byToken(buffer, config, &cache).array();
|
||||||
auto depths = generateDepthInfo(tokens);
|
auto depths = generateDepthInfo(tokens);
|
||||||
auto tokenFormatter = TokenFormatter!OutputRange(tokens, depths, output,
|
auto tokenFormatter = TokenFormatter!OutputRange(buffer, tokens, depths, output,
|
||||||
&astInformation, formatterConfig);
|
&astInformation, formatterConfig);
|
||||||
tokenFormatter.format();
|
tokenFormatter.format();
|
||||||
}
|
}
|
||||||
|
@ -74,9 +74,10 @@ struct TokenFormatter(OutputRange)
|
||||||
* astInformation = information about the AST used to inform formatting
|
* astInformation = information about the AST used to inform formatting
|
||||||
* decisions.
|
* decisions.
|
||||||
*/
|
*/
|
||||||
this(const(Token)[] tokens, immutable short[] depths, OutputRange output,
|
this(const ubyte[] rawSource, const(Token)[] tokens, immutable short[] depths,
|
||||||
ASTInformation* astInformation, Config* config)
|
OutputRange output, ASTInformation* astInformation, Config* config)
|
||||||
{
|
{
|
||||||
|
this.rawSource = rawSource;
|
||||||
this.tokens = tokens;
|
this.tokens = tokens;
|
||||||
this.depths = depths;
|
this.depths = depths;
|
||||||
this.output = output;
|
this.output = output;
|
||||||
|
@ -105,6 +106,9 @@ private:
|
||||||
/// Output to write output to
|
/// Output to write output to
|
||||||
OutputRange output;
|
OutputRange output;
|
||||||
|
|
||||||
|
/// Used for skipping parts of the file with `dfmt off` and `dfmt on` comments
|
||||||
|
const ubyte[] rawSource;
|
||||||
|
|
||||||
/// Tokens being formatted
|
/// Tokens being formatted
|
||||||
const Token[] tokens;
|
const Token[] tokens;
|
||||||
|
|
||||||
|
@ -234,8 +238,43 @@ private:
|
||||||
writeToken();
|
writeToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string commentText(size_t i)
|
||||||
|
{
|
||||||
|
import std.string : strip;
|
||||||
|
assert(tokens[i].type == tok!"comment");
|
||||||
|
string commentText = tokens[i].text;
|
||||||
|
if (commentText[0 ..2] == "//")
|
||||||
|
commentText = commentText[2 .. $];
|
||||||
|
else
|
||||||
|
commentText = commentText[2 .. $ - 2];
|
||||||
|
return commentText.strip();
|
||||||
|
}
|
||||||
|
|
||||||
|
void skipFormatting()
|
||||||
|
{
|
||||||
|
size_t dfmtOff = index;
|
||||||
|
size_t dfmtOn = index;
|
||||||
|
foreach (i; dfmtOff + 1.. tokens.length)
|
||||||
|
{
|
||||||
|
dfmtOn = i;
|
||||||
|
if (tokens[i].type != tok!"comment")
|
||||||
|
continue;
|
||||||
|
immutable string commentText = commentText(i);
|
||||||
|
if (commentText == "dfmt on")
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
write(cast(string) rawSource[tokens[dfmtOff].index .. tokens[dfmtOn].index]);
|
||||||
|
index = dfmtOn;
|
||||||
|
}
|
||||||
|
|
||||||
void formatComment()
|
void formatComment()
|
||||||
{
|
{
|
||||||
|
if (commentText(index) == "dfmt off")
|
||||||
|
{
|
||||||
|
skipFormatting();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
immutable bool currIsSlashSlash = tokens[index].text[0 .. 2] == "//";
|
immutable bool currIsSlashSlash = tokens[index].text[0 .. 2] == "//";
|
||||||
immutable prevTokenEndLine = index == 0 ? size_t.max : tokenEndLine(tokens[index - 1]);
|
immutable prevTokenEndLine = index == 0 ? size_t.max : tokenEndLine(tokens[index - 1]);
|
||||||
immutable size_t currTokenLine = tokens[index].line;
|
immutable size_t currTokenLine = tokens[index].line;
|
||||||
|
|
|
@ -144,7 +144,7 @@ else
|
||||||
|
|
||||||
private void printHelp()
|
private void printHelp()
|
||||||
{
|
{
|
||||||
writeln(`dfmt 0.3.4
|
writeln(`dfmt 0.4.0-alpha
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--help | -h Print this help message
|
--help | -h Print this help message
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
void main(string[] args)
|
||||||
|
{
|
||||||
|
// dfmt off
|
||||||
|
getopt(args,
|
||||||
|
"optionOne", &optionOne,
|
||||||
|
"optionTwo", &optionTwo,
|
||||||
|
"optionThree", &optionThree);
|
||||||
|
// dfmt on
|
||||||
|
|
||||||
|
getopt(args, "optionOne", &optionOne, "optionTwo", &optionTwo, "optionThree", &optionThree);
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
void main(string[] args)
|
||||||
|
{
|
||||||
|
// dfmt off
|
||||||
|
getopt(args,
|
||||||
|
"optionOne", &optionOne,
|
||||||
|
"optionTwo", &optionTwo,
|
||||||
|
"optionThree", &optionThree);
|
||||||
|
// dfmt on
|
||||||
|
|
||||||
|
getopt(args, "optionOne", &optionOne, "optionTwo", &optionTwo, "optionThree", &optionThree);
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
void main(string[] args) {
|
||||||
|
// dfmt off
|
||||||
|
getopt(args,
|
||||||
|
"optionOne", &optionOne,
|
||||||
|
"optionTwo", &optionTwo,
|
||||||
|
"optionThree", &optionThree);
|
||||||
|
// dfmt on
|
||||||
|
|
||||||
|
getopt(args, "optionOne", &optionOne, "optionTwo", &optionTwo, "optionThree", &optionThree);
|
||||||
|
}
|
Loading…
Reference in New Issue