Skip to content

If constraints: check for same indent level#394

Closed
wilzbach wants to merge 2 commits intodlang-community:masterfrom
wilzbach:if_constraints_same_indent
Closed

If constraints: check for same indent level#394
wilzbach wants to merge 2 commits intodlang-community:masterfrom
wilzbach:if_constraints_same_indent

Conversation

@wilzbach
Copy link
Member

See also: dlang/dlang.org#1534

This is based on #348 as the sourceCode is needed to count the whitespace tokens at the beginning of the line.

@wilzbach wilzbach changed the title If constraints same indent If constraints: check for same indent level Dec 26, 2016
@wilzbach wilzbach force-pushed the if_constraints_same_indent branch from 4012383 to 3ab6368 Compare December 26, 2016 12:37
@andralex
Copy link

I see you also made some refactorings here - nice.

@andralex
Copy link

cch @Hackerpilot :)

@wilzbach
Copy link
Member Author

I see you also made some refactorings here - nice.

This is only #348 as this needs access to the raw sourceCode array to count the indentation level.

- add allman style check
- detect trailing whitespace
- check for two or more consecutive empty lines
@wilzbach wilzbach force-pushed the if_constraints_same_indent branch from 3ab6368 to 174139d Compare February 15, 2017 23:10
@wilzbach
Copy link
Member Author

@Hackerpilot I am a bit stuck here.
As you stated on #348 you would prefer not to expose the raw source code to the plugins, which is
understandable. However, with only the token array at hand, how can I look ahead/back to the next/previous token (apart from iterating through the tokens array all the time)?
From what I learned the token doesn't know its position within the array, it only knows about:

type = the token type
text = the text of the token, which may be null
line = the line number at which this token occurs
column = the column number at which this token occurs
index = the byte offset from the beginning of the input at which this token occurs

Or to be more precise, for the indentation check the plugin looks at a couple of *Declarations in the AST. Then it compares the indent level of its constraint with the indent of the name token itself. For example for FunctionDeclaration:

override void visit(const FunctionDeclaration decl) {
	if (decl.constraint !is null)
		checkConstraintSpace(decl.constraint, decl.name);
}

Currently checkConstraintSpace is done by looking back until the beginning of the line and counting the seen whitespace characters, which is the indented level of the token:

auto lineIndent = code[0 .. constraint.location].retro
				.until('\n')
				.walkLength;

auto tokenLineStart = code[0 .. location].retro
						.until('\n')
						.walkLength;

auto tokenIndent = code[location - tokenLineStart .. location]
					.until!(x => !x.isWhite)
					.walkLength;

if (tokenIndent != lineIndent)
	addErrorMessage(constraint.expression.line, constraint.expression.column, KEY, MESSAGE);

How would I do this with merely the token array? Or can I (as proposed before) just pass the code array into the plugin (it's already in memory anyways).

@ghost
Copy link

ghost commented Feb 24, 2017

@wilzbach

@andralex
Copy link

@wilzbach @Hackerpilot I see this needs some rebasing, right?

@wilzbach
Copy link
Member Author

@wilzbach @Hackerpilot I see this needs some rebasing, right?

Superseded by #450

@wilzbach wilzbach closed this Jun 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants