-
Notifications
You must be signed in to change notification settings - Fork 54
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
Klokane/annotation with column line info #653
Conversation
src/RefractAPI.cc
Outdated
@@ -26,6 +26,8 @@ | |||
#include "NamedTypesRegistry.h" | |||
#include "ConversionContext.h" | |||
|
|||
#include "reporting.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove - restful
src/RefractSourceMap.cc
Outdated
@@ -1,4 +1,6 @@ | |||
#include "RefractSourceMap.h" | |||
#include "reporting.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove - restful
src/reporting.h
Outdated
@@ -11,6 +11,7 @@ | |||
|
|||
#include "drafter.h" | |||
#include "SourceAnnotation.h" | |||
#include "reporting.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove - mistake
test/draftertest.h
Outdated
@@ -103,10 +103,11 @@ namespace draftertest | |||
|
|||
struct FixtureHelper { | |||
|
|||
static std::unique_ptr<refract::IElement> parseAndSerialize( | |||
snowcrash::ParseResult<snowcrash::Blueprint>& blueprint, const drafter::WrapperOptions& options) | |||
static std::unique_ptr<refract::IElement> parseAndSerialize(const std::string& source, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove - place inside handleResultJSON()
tools/update-fixtures.sh
Outdated
@@ -0,0 +1,19 @@ | |||
#!/bin/sh | |||
|
|||
for i in $(./bin/test-libdrafter | grep Filename | sed -e 's/^ Filename: //' | sed -e 's/\x1B\[[0-9;]*[JKmsu]//g') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can already do env GENERATE=1 make test
to update fixtures, was you unaware of that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I remember there were some discussion related it is not working.
So I probably miss info it is working again.
Ok, so i will remove this script.
59b01cb
to
5273e8d
Compare
5273e8d
to
3217ac2
Compare
@apiaryio/adt ready for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line and column info should be optional (a new option to drafter_parse) and it should be defaulted to false
. @kylef Can you confirm?
We will have no need to change all the existing fixtures.
{ \ | ||
REQUIRE(FixtureHelper::handleResultJSON(wrapper, "test/fixtures/" category "/" name, options, mustBeOk)); \ | ||
REQUIRE(FixtureHelper::handleResultJSON("test/fixtures/mson/" name, MSONTestOptions, mustBeOk)); \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like below,
- MSONTestOptions
+ drafter::WrapperOptions(false, true)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not seen in assignment it should be optional. Assignment reference fury-adapter-swagger apiaryio/fury-adapter-swagger#211
It is reason why I mean it should not be optional, but if it should be it is not problem to adapt it.
In the Swagger 2 parser line and number is always there. I don't see any benefit of it being optional because this is something that most consumers want. Do you have some use-case in mind where you would want to disable line and column numbers for annotation source maps? |
}, | ||
"column": { | ||
"element": "number", | ||
"content": 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per the spec, the column should be zero-indexed. Since this is empty line this should be 0?
https://api-elements.readthedocs.io/en/latest/element-definitions.html#source-map
Also, given the ADD I think the source map should not end on the start of line 9, but end of line 8? Maybe larger issue to deal with than in the scope of this PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use algorithm which was already there for -l
switch. So it is wrong in original code.
I will look at it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I don't see where in the spec the line or column is said to be zero-indexed. Only the byte offset of the source map is said to be zero-indexed. The column
and line
attributes are actually very undefined per spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, you are right. We should clarify in the spec. Should we go for 1-indexed or 0-indexed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
column and line numbers are good old natural numbers, and I would keep that. Natural numbers, in my world, start from 1.
src/reporting.cc
Outdated
@@ -134,7 +70,7 @@ namespace | |||
if (useLineNumbers) { | |||
|
|||
AnnotationPosition annotationPosition; | |||
GetLineFromMap(linesEndIndex, *it, annotationPosition); | |||
::GetLineFromMap(linesEndIndex, *it, annotationPosition); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to use a using
declaration or qualify by namespace instead of referencing a global symbol via ::
prefix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be removed, that is rest after some "experiments" :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from pointed out improvements, please introduce unit tests.
src/SourceMapUtils.h
Outdated
* \param range Character index mapping as input | ||
* \param out Position of the given range as output | ||
*/ | ||
void GetLineFromMap(const std::vector<size_t>& linesEndIndex, const mdp::Range& range, AnnotationPosition& out); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is out
not returned by value here? It's just four integers.
src/SourceMapUtils.h
Outdated
* \param source Source data | ||
* \param out Vector containing indexes of all end line character in source | ||
*/ | ||
void GetLinesEndIndex(const std::string& source, NewLinesIndex& out); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to fill a vector? Would it be possible/desirable to provide a callback called instead of push_back
ing?
src/SourceMapUtils.cc
Outdated
out.toColumn = 0; | ||
|
||
// Finds starting line and column position | ||
annotationPositionIt = std::upper_bound(linesEndIndex.begin(), linesEndIndex.end(), range.location) - 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few suggestions:
auto annotationPosition = std::upper_bound(...) ...;
instead of declaration on line10
.out
is completely reset on lines12
-15
; consider returningAnnotationPosition
instead.linesEndIndex
is a vector, but this is not vector-specific code; maybe use iterator templates instead?- the
- 1
at the end of line18
is something I have noticed after third reading. Please usestd::prev
orstd::advance
to both make the operation more visible and generalize to non-random_access_iterator
.
src/SourceMapUtils.cc
Outdated
out.toLine = std::distance(linesEndIndex.begin(), annotationPositionIt) + 1; | ||
out.toColumn = (range.location + range.length) - *annotationPositionIt + 1; | ||
|
||
if (*(annotationPositionIt + 1) == (range.location + range.length)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggesting std::next
instead of + 1
on iterator
src/SourceMapUtils.h
Outdated
|
||
/** structure contains starting and ending position of a error/warning. */ | ||
struct AnnotationPosition { | ||
size_t fromLine; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a default initializer for each member (size_t fromLine = 0
).
src/SourceMapUtils.h
Outdated
namespace drafter | ||
{ | ||
|
||
typedef std::vector<size_t> NewLinesIndex; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer using
declaration instead of typedef
s.
When people need sourcemaps to manipulate stuff but it's not a user interface so they don't need line and column info. |
Oh, I realize code will not working correctly with |
fe87c93
to
4e990f8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well addressed, just two more little-things.
@@ -34,7 +38,10 @@ namespace drafter | |||
return registry; | |||
} | |||
|
|||
ConversionContext(const WrapperOptions& options) : options(options) {} | |||
inline const NewLinesIndex& GetNewLinesIndex() const |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why inline
? Fun fact: inline
causes GCC to produce a weak symbol, that's all.
4e990f8
to
3c9acd8
Compare
We still haven't made the column/line info optional |
CHANGELOG.md
Outdated
## master | ||
|
||
* Add column/line info to anotations source maps | ||
* change start column index from 1 to 0 in reporting. (while `-l` switch is used) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this done? This is UI. We need to use index starting from 1. @kylef What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you mean the -u
flag?
-u, --use-line-num use line and row number instead of character index when printing annotation
I don't think this is nessecerily done in the UI, it is an explanation that -u
flag will now behave differently than before because the same logic is used for column/line numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should -u
use 1-indexed or 0-indexed? Looking at other CLI tools it seems 1-indexed is the standard but this is up for discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on my reading of the spec Pavan raised a valid point. There is actually no need to change to zero-based indexing for column and line attributes of source maps; I initially also misread the spec, because it talks about zero-based indexing - of the byte offset however, not column and line numbers. All in all, column and line numbers are good old natural numbers, and I would keep that. Natural numbers, in my world, start from 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so do you want to return back implementation to one-indexed like in original code?
I made unit tests related to indexing 25 days ago. It was clear from this code how indexing will be implemented. I asked for review, nobody commented it...
f255865
Yes, we did not it optional. |
I was convinced we agreed not to make it optional. If output size is a worry, we should tackle the problem more head on, providing a flag for output size optimization. Maybe another PR "Add flag to disable column/line emission in source maps"? |
3c9acd8
to
42e55bd
Compare
@apiaryio/adt so it is updated against master (include cmake build) and colums are indexed from 1. |
Source code ready for review