apidash icon indicating copy to clipboard operation
apidash copied to clipboard

Feat syntax highlight request body

Open Tanish2002 opened this issue 10 months ago • 8 comments

PR Description

This PR Introduces syntax highlighting for JSON type request body. We had a comment referencing the incomplete JsonTextFieldEditor implementation. Instead of hacking together our own, I've used a third-party package code_field.

This package provides syntax highlighting as well as inserts tab characters at appropriate positions. We can also use this same package in the future for other request bodies such as Graphql, protobuf, etc. Once we support them.

I've also changed the default TextFieldEditor widget using the plaintext theme, as the name suggests, it doesn't provide any syntax highlighting but does provide simple tab character insertion wherever needed.

Here is a list of currently supported languages and themes that are supported: languages, themes

Lastly Here are some screenshots: image image

I've decided to use monokaiSublimeTheme for dark mode and defaultTheme for light mode since they look the most natural for our colorscheme.

If unsatisfied with the current choice please tell me any other theme from the list above, The developer has also created a nice site to figure out a theme: Here

Checklist

  • [x] I have gone through the contributing guide
  • [x] I have updated my branch and synced it with project main branch before making this PR
  • [x] I have run the tests (flutter test) and all tests are passing

Added/updated tests?

  • [x] Yes

Tanish2002 avatar Mar 25 '24 13:03 Tanish2002

We already have dark & light syntax highlighting themes for API Dash which is used in codegens here -https://github.com/foss42/apidash/blob/f3a7865459e3f0b2b44b9012026fe71159d8897d/lib/consts.dart#L160

animator avatar Mar 25 '24 14:03 animator

@animator I missed it 😅, I've pushed a fix to address this: New screenshots: image image

Tanish2002 avatar Mar 25 '24 14:03 Tanish2002

You need to go through the relevant issue #22 which talks about the deliverables and the past work & the issues in the WIP PR that was merged.

animator avatar Mar 26 '24 10:03 animator

@animator As per the issue, I believe syntax highlight is taken care of through this PR.

I should be able to add formatting relatively easily(using kEncoder.convert), and validation should also be possible. However, I would need some help with the UI.

I added a "format JSON" elevated button. It looks like this: image Do you think this is fine? or I should do something else.

And, where do I keep the validation error? I'm thinking of simply adding a check to the OnChanged hook that will check if JSON is valid or not. If not then it would show an error. But not sure where to place this widget in the UI

Also, do I create a new file inside utils folder (json_utils.dart) for these json functions for validation, and formatting or just add methods to the JsonTextFieldEditor class?

Tanish2002 avatar Mar 26 '24 11:03 Tanish2002

@animator This is what I've come up with. Haven't added any new packages, simply using the json function from dart:convert for formatting as well as validation issues.

https://github.com/foss42/apidash/assets/55488165/ce121489-1035-497b-9a8f-859d6dff44cb

Tanish2002 avatar Mar 26 '24 12:03 Tanish2002

@animator pushed the new changes, made it look pretty as much as I could 😅. You can check the following video for a example. https://github.com/foss42/apidash/assets/55488165/22ebac0d-eb0c-4ab6-bf4f-b674f1fc43ab

Tanish2002 avatar Mar 26 '24 16:03 Tanish2002

The original text editor is perfectly fine. Why was it modified? Only JSON editor requires modification.

animator avatar Mar 28 '24 14:03 animator

The original text editor is perfectly fine. Why was it modified? Only JSON editor requires modification.

Yeah,

  • I modified the text editor class to ensure consistency between all editor widgets and make it easier for other developers to understand.
  • I used plaintext as the language to remove highlighting. https://github.com/foss42/apidash/blob/4912f71a77159a67704e413319b6ef5b90ca5e7a/lib/widgets/editor.dart#L33
  • I believe in using existing packages (which I added in this PR) for consistent code rather than creating custom implementations for every editor type.
  • I haven't implemented this but I think an abstract Editor class (using the CodeField widget) offers flexibility. Subclasses for various editors(json, graphql, protobuf, etc) can then add features like formatting and validation as needed.

@animator If you believe this isn't what we should do, then I can go ahead and revert the changes for that.

Tanish2002 avatar Mar 29 '24 22:03 Tanish2002