HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Tool for creating CodeReview questions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
codereviewcreatingquestionsfortool

Problem

To download the tool and for usage instructions, see the follow up to this question

Description

I have realized that there's a whole bunch of code I want to have reviewed, but copying file by file, selecting the code and pressing Ctrl + K is a slow process, and I want to remember which other parts to include in my question, so I decided to make a tool for it. I thought it could be useful, especially since most of my questions are structured in very similar ways already...

This code is for inputting a couple of files, and outputting a CR question stub, ready to be filled in with details. The code automatically formats the input files to match StackExchange formatting, with four spaces added in front of each line (No more Ctrl + K !!)

It is very likely that you will see more questions from me structured in the same way as this question is. And if you want to use it yourself, feel free to do so.

Related previously existing tools:

  • Tool for automatically correcting indentation and formatting of CR & SO code



  • Bookmarklet for selecting code snippets on Code Review



Code download

For your convenience, this code can be found on GitHub (Many thanks to @amon who taught me a lot more about how to use git)

Class Summary (10079 bytes in 342 lines in 4 files)

  • CountingStream: OutputStream that keeps track on the number of written bytes to it



  • ReviewPrepareFrame: JFrame for letting user select files that should be up for review



  • ReviewPreparer: The most important class, takes care of most of the work. Expects a List of files in the constructor and an OutputStream when called.



  • TextAreaOutputStream: OutputStream for outputting to a JTextArea.



Code

CountingStream: (approximately 679 bytes in 27 lines)

```
/**
* An output stream that keeps track of how many bytes that has been written to it.
*/
public class CountingStream extends FilterOutputStream {
private final AtomicInteger bytesWritten;

public CountingStream(OutputStream out) {
super(ou

Solution

I would encourage you to produce more explicit output, particularly with the filenames. If I wanted to reverse the process and scrape the code into files on my machine, using a Python script such as the following…

import json
from lxml import html
import re
import requests

FILENAME_HINT_XPATH = "../preceding-sibling::p[1]/strong/text()"

def code_for_post(site, post):
r = requests.get('https://api.stackexchange.com/2.1/posts/{1}?site={0}&filter=withbody'.format(site, post))
j = json.loads(r.text)
body = j['items'][0]['body']
tree = html.fromstring(body)

code_elements = tree.xpath("//pre/code[%s]" % (FILENAME_HINT_XPATH))
return dict((c.xpath(FILENAME_HINT_XPATH)[0], c.findtext(".")) for c in code_elements)

def write_files(code):
extension = '.java' # >f, content

write_files(code_for_post('codereview', 41198))


… then I would have to make assumptions about the filename extension.

The invocation method could be improved. Instead of hard-coding a particular directory to look in for the source files, I would suggest…

  • If files are explicitly passed to the program as command-line arguments, use those files.



  • If a directory is specified, then use all files contained therein, excluding files with significant non-ASCII content.



  • If no command-line arguments are used, then operate on the current directory.



It would be nice to be able to say java ReviewPreparer *.java |pbcopy.

Context

StackExchange Code Review Q#41198, answer score: 17

Revisions (0)

No revisions yet.