Skip to content
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

Merged
merged 16 commits into from
Nov 15, 2018

Conversation

klokane
Copy link
Contributor

@klokane klokane commented Oct 2, 2018

Source code ready for review

@@ -26,6 +26,8 @@
#include "NamedTypesRegistry.h"
#include "ConversionContext.h"

#include "reporting.h"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove - restful

@@ -1,4 +1,6 @@
#include "RefractSourceMap.h"
#include "reporting.h"
Copy link
Contributor Author

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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove - mistake

@@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove - place inside handleResultJSON()

@@ -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')
Copy link
Member

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?

Copy link
Contributor Author

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.

@klokane klokane force-pushed the klokane/annotation-with-column-line-info branch from 59b01cb to 5273e8d Compare October 2, 2018 20:51
@klokane klokane force-pushed the klokane/annotation-with-column-line-info branch from 5273e8d to 3217ac2 Compare October 2, 2018 20:56
@klokane
Copy link
Contributor Author

klokane commented Oct 2, 2018

@apiaryio/adt ready for review

Copy link
Contributor

@pksunkara pksunkara left a 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)); \
Copy link
Contributor

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)

Copy link
Contributor Author

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.

@kylef
Copy link
Member

kylef commented Oct 5, 2018

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?

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
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Member

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#653 (comment)

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);
Copy link
Contributor

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?

Copy link
Contributor Author

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" :)

Copy link
Contributor

@tjanc tjanc left a 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.

* \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);
Copy link
Contributor

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.

* \param source Source data
* \param out Vector containing indexes of all end line character in source
*/
void GetLinesEndIndex(const std::string& source, NewLinesIndex& out);
Copy link
Contributor

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_backing?

out.toColumn = 0;

// Finds starting line and column position
annotationPositionIt = std::upper_bound(linesEndIndex.begin(), linesEndIndex.end(), range.location) - 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions:

  1. auto annotationPosition = std::upper_bound(...) ...; instead of declaration on line 10.
  2. out is completely reset on lines 12-15; consider returning AnnotationPosition instead.
  3. linesEndIndex is a vector, but this is not vector-specific code; maybe use iterator templates instead?
  4. the - 1 at the end of line 18 is something I have noticed after third reading. Please use std::prev or std::advance to both make the operation more visible and generalize to non-random_access_iterator.

out.toLine = std::distance(linesEndIndex.begin(), annotationPositionIt) + 1;
out.toColumn = (range.location + range.length) - *annotationPositionIt + 1;

if (*(annotationPositionIt + 1) == (range.location + range.length)) {
Copy link
Contributor

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


/** structure contains starting and ending position of a error/warning. */
struct AnnotationPosition {
size_t fromLine;
Copy link
Contributor

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 Show resolved Hide resolved
namespace drafter
{

typedef std::vector<size_t> NewLinesIndex;
Copy link
Contributor

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 typedefs.

src/SourceMapUtils.h Show resolved Hide resolved
@pksunkara
Copy link
Contributor

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?

When people need sourcemaps to manipulate stuff but it's not a user interface so they don't need line and column info.

@klokane
Copy link
Contributor Author

klokane commented Oct 22, 2018

Oh, I realize code will not working correctly with utf-8 because it count byte positions instead of char positions. Will going to fix it.

@klokane klokane force-pushed the klokane/annotation-with-column-line-info branch 4 times, most recently from fe87c93 to 4e990f8 Compare November 6, 2018 12:58
tjanc
tjanc previously requested changes Nov 8, 2018
Copy link
Contributor

@tjanc tjanc left a 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
Copy link
Contributor

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.

CHANGELOG.md Outdated Show resolved Hide resolved
@klokane klokane force-pushed the klokane/annotation-with-column-line-info branch from 4e990f8 to 3c9acd8 Compare November 8, 2018 16:06
@pksunkara
Copy link
Contributor

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)
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/apiaryio/drafter/pull/653/files/3217ac2c099085b7bfd2640e1ad91751287697e2#r223059516

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.

Copy link
Contributor Author

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

@klokane
Copy link
Contributor Author

klokane commented Nov 9, 2018

Yes, we did not it optional.
There were discussion related to this and there was no decision make it optional

@tjanc
Copy link
Contributor

tjanc commented Nov 9, 2018

We still haven't made the column/line info 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"?

@klokane klokane force-pushed the klokane/annotation-with-column-line-info branch from 3c9acd8 to 42e55bd Compare November 13, 2018 12:54
@klokane
Copy link
Contributor Author

klokane commented Nov 13, 2018

@apiaryio/adt so it is updated against master (include cmake build) and colums are indexed from 1.
So please for review

@klokane klokane merged commit 1facd70 into master Nov 15, 2018
@klokane klokane deleted the klokane/annotation-with-column-line-info branch November 15, 2018 15:07
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.

4 participants