aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PscIde.js
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2016-06-12 15:37:34 -0400
committereric thul <thul.eric@gmail.com>2016-06-12 15:37:34 -0400
commit055d127bb9ca88c9bfcc7286a33f36f5ff6b4462 (patch)
tree57ca13847432100a9b35957f51526f5f555c1bd2 /src/PscIde.js
parent531c751fe5593750a377db38bcfaf9a5383ac661 (diff)
downloadpurs-loader-055d127bb9ca88c9bfcc7286a33f36f5ff6b4462.tar.gz
purs-loader-055d127bb9ca88c9bfcc7286a33f36f5ff6b4462.tar.zst
purs-loader-055d127bb9ca88c9bfcc7286a33f36f5ff6b4462.zip
Handle no filename in PscIde result
Resolves #58
Diffstat (limited to 'src/PscIde.js')
-rw-r--r--src/PscIde.js92
1 files changed, 49 insertions, 43 deletions
diff --git a/src/PscIde.js b/src/PscIde.js
index d99b639..9d6c1ff 100644
--- a/src/PscIde.js
+++ b/src/PscIde.js
@@ -178,54 +178,60 @@ function rebuild(psModule) {
178module.exports.rebuild = rebuild; 178module.exports.rebuild = rebuild;
179 179
180function formatIdeResult(result, options, index, length) { 180function formatIdeResult(result, options, index, length) {
181 const srcPath = path.relative(options.context, result.filename)
182 const pos = result.position
183 const fileAndPos = `${srcPath}:${pos.startLine}:${pos.startColumn}`
184 let numAndErr = `[${index+1}/${length} ${result.errorCode}]` 181 let numAndErr = `[${index+1}/${length} ${result.errorCode}]`
185 numAndErr = options.pscIdeColors ? colors.yellow(numAndErr) : numAndErr 182 numAndErr = options.pscIdeColors ? colors.yellow(numAndErr) : numAndErr
186 183
187 return fs.readFileAsync(result.filename, 'utf8').then(source => { 184 function makeResult() {
188 const lines = source.split('\n').slice(pos.startLine - 1, pos.endLine) 185 return Promise.resolve(`\n${numAndErr} ${result.message}`)
189 const endsOnNewline = pos.endColumn === 1 && pos.startLine !== pos.endLine 186 }
190 const up = options.pscIdeColors ? colors.red('^') : '^' 187
191 const down = options.pscIdeColors ? colors.red('v') : 'v' 188 function makeResultSnippet(filename, pos) {
192 let trimmed = lines.slice(0) 189 const srcPath = path.relative(options.context, filename);
193 190 const fileAndPos = `${srcPath}:${pos.startLine}:${pos.startColumn}`
194 if (endsOnNewline) { 191
195 lines.splice(lines.length - 1, 1) 192 return fs.readFileAsync(filename, 'utf8').then(source => {
196 pos.endLine = pos.endLine - 1 193 const lines = source.split('\n').slice(pos.startLine - 1, pos.endLine)
197 pos.endColumn = lines[lines.length - 1].length || 1 194 const endsOnNewline = pos.endColumn === 1 && pos.startLine !== pos.endLine
198 } 195 const up = options.pscIdeColors ? colors.red('^') : '^'
196 const down = options.pscIdeColors ? colors.red('v') : 'v'
197 let trimmed = lines.slice(0)
198
199 if (endsOnNewline) {
200 lines.splice(lines.length - 1, 1)
201 pos.endLine = pos.endLine - 1
202 pos.endColumn = lines[lines.length - 1].length || 1
203 }
199 204
200 // strip newlines at the end 205 // strip newlines at the end
201 if (endsOnNewline) { 206 if (endsOnNewline) {
202 trimmed = lines.reverse().reduce((trimmed, line, i) => { 207 trimmed = lines.reverse().reduce((trimmed, line, i) => {
203 if (i === 0 && line === '') trimmed.trimming = true 208 if (i === 0 && line === '') trimmed.trimming = true
204 if (!trimmed.trimming) trimmed.push(line) 209 if (!trimmed.trimming) trimmed.push(line)
205 if (trimmed.trimming && line !== '') { 210 if (trimmed.trimming && line !== '') {
206 trimmed.trimming = false 211 trimmed.trimming = false
207 trimmed.push(line) 212 trimmed.push(line)
208 } 213 }
209 return trimmed 214 return trimmed
210 }, []).reverse() 215 }, []).reverse()
211 pos.endLine = pos.endLine - (lines.length - trimmed.length) 216 pos.endLine = pos.endLine - (lines.length - trimmed.length)
212 pos.endColumn = trimmed[trimmed.length - 1].length || 1 217 pos.endColumn = trimmed[trimmed.length - 1].length || 1
213 } 218 }
214 219
215 const spaces = ' '.repeat(String(pos.endLine).length) 220 const spaces = ' '.repeat(String(pos.endLine).length)
216 let snippet = trimmed.map((line, i) => { 221 let snippet = trimmed.map((line, i) => {
217 return ` ${pos.startLine + i} ${line}` 222 return ` ${pos.startLine + i} ${line}`
218 }).join('\n') 223 }).join('\n')
219 224
220 if (trimmed.length === 1) { 225 if (trimmed.length === 1) {
221 snippet += `\n ${spaces} ${' '.repeat(pos.startColumn - 1)}${up.repeat(pos.endColumn - pos.startColumn + 1)}` 226 snippet += `\n ${spaces} ${' '.repeat(pos.startColumn - 1)}${up.repeat(pos.endColumn - pos.startColumn + 1)}`
222 } else { 227 } else {
223 snippet = ` ${spaces} ${' '.repeat(pos.startColumn - 1)}${down}\n${snippet}` 228 snippet = ` ${spaces} ${' '.repeat(pos.startColumn - 1)}${down}\n${snippet}`
224 snippet += `\n ${spaces} ${' '.repeat(pos.endColumn - 1)}${up}` 229 snippet += `\n ${spaces} ${' '.repeat(pos.endColumn - 1)}${up}`
225 } 230 }
226 231
227 return Promise.resolve( 232 return Promise.resolve(`\n${numAndErr} ${fileAndPos}\n\n${snippet}\n\n${result.message}`)
228 `\n${numAndErr} ${fileAndPos}\n\n${snippet}\n\n${result.message}` 233 })
229 ) 234 }
230 }) 235
236 return result.filename && result.position ? makeResultSnippet(result.filename, result.position) : makeResult();
231} 237}