feat(parser/diff): support max diff lines
This commit is contained in:
		
							parent
							
								
									bcaed579e5
								
							
						
					
					
						commit
						5fd7c19783
					
				|  | @ -30,6 +30,7 @@ type Conf struct { | ||||||
| 			CompareSpace    bool | 			CompareSpace    bool | ||||||
| 			AlwaysHide      bool | 			AlwaysHide      bool | ||||||
| 			ForceQuitOnDiff bool | 			ForceQuitOnDiff bool | ||||||
|  | 			MaxDiffLines    int | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | @ -60,8 +61,8 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) ( | ||||||
| 			} | 			} | ||||||
| 			slog.Debug("compare", "filename", output.FileName, | 			slog.Debug("compare", "filename", output.FileName, | ||||||
| 				"answer path", output.AnswerPath, | 				"answer path", output.AnswerPath, | ||||||
| 				"actual", result.Files[output.FileName], | 				"actual length", len(result.Files[output.FileName]), | ||||||
| 				"answer", string(answer)) | 				"answer length", len(string(answer))) | ||||||
| 			// If no difference, assign score
 | 			// If no difference, assign score
 | ||||||
| 			if compareChars(string(answer), result.Files[output.FileName], | 			if compareChars(string(answer), result.Files[output.FileName], | ||||||
| 				output.CompareSpace) { | 				output.CompareSpace) { | ||||||
|  | @ -85,7 +86,7 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) ( | ||||||
| 
 | 
 | ||||||
| 					// Generate diff block with surrounding context
 | 					// Generate diff block with surrounding context
 | ||||||
| 					diffOutput := generateDiffWithContext( | 					diffOutput := generateDiffWithContext( | ||||||
| 						stdoutLines, resultLines, diffOps) | 						stdoutLines, resultLines, diffOps, output.MaxDiffLines) | ||||||
| 					diffOutput = strings.TrimSuffix(diffOutput, "\n  \n") | 					diffOutput = strings.TrimSuffix(diffOutput, "\n  \n") | ||||||
| 					comment += fmt.Sprintf( | 					comment += fmt.Sprintf( | ||||||
| 						"```diff\n%s\n```\n", | 						"```diff\n%s\n```\n", | ||||||
|  | @ -226,32 +227,38 @@ func reverse(s []operation) []operation { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // generateDiffWithContext creates a diff block with surrounding context from stdout and result.
 | // generateDiffWithContext creates a diff block with surrounding context from stdout and result.
 | ||||||
| func generateDiffWithContext(stdoutLines, resultLines []string, ops []operation) string { | func generateDiffWithContext( | ||||||
|  | 	stdoutLines, resultLines []string, ops []operation, maxDiffLines int, | ||||||
|  | ) string { | ||||||
| 	var diffBuilder strings.Builder | 	var diffBuilder strings.Builder | ||||||
| 
 | 
 | ||||||
| 	srcIndex, dstIndex := 0, 0 | 	srcIndex, dstIndex, lineCount := 0, 0, 0 | ||||||
| 
 | 
 | ||||||
| 	for _, op := range ops { | 	for _, op := range ops { | ||||||
| 		switch op { | 		switch op { | ||||||
| 		case INSERT: | 		case INSERT: | ||||||
| 			if dstIndex < len(resultLines) { | 			if dstIndex < len(resultLines) { | ||||||
| 				diffBuilder.WriteString(fmt.Sprintf("+ %s\n", resultLines[dstIndex])) | 				diffBuilder.WriteString(fmt.Sprintf("+ %s\n", resultLines[dstIndex])) | ||||||
| 				dstIndex++ | 				dstIndex += 1 | ||||||
|  | 				lineCount += 1 | ||||||
| 			} | 			} | ||||||
| 
 |  | ||||||
| 		case MOVE: | 		case MOVE: | ||||||
| 			if srcIndex < len(stdoutLines) { | 			if srcIndex < len(stdoutLines) { | ||||||
| 				diffBuilder.WriteString(fmt.Sprintf("  %s\n", stdoutLines[srcIndex])) | 				diffBuilder.WriteString(fmt.Sprintf("  %s\n", stdoutLines[srcIndex])) | ||||||
| 				srcIndex++ | 				srcIndex += 1 | ||||||
| 				dstIndex++ | 				dstIndex += 1 | ||||||
|  | 				lineCount += 1 | ||||||
| 			} | 			} | ||||||
| 
 |  | ||||||
| 		case DELETE: | 		case DELETE: | ||||||
| 			if srcIndex < len(stdoutLines) { | 			if srcIndex < len(stdoutLines) { | ||||||
| 				diffBuilder.WriteString(fmt.Sprintf("- %s\n", stdoutLines[srcIndex])) | 				diffBuilder.WriteString(fmt.Sprintf("- %s\n", stdoutLines[srcIndex])) | ||||||
| 				srcIndex++ | 				srcIndex += 1 | ||||||
|  | 				lineCount += 1 | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | 		if maxDiffLines > 0 && lineCount >= maxDiffLines { | ||||||
|  | 			break | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return diffBuilder.String() | 	return diffBuilder.String() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user