|
174 | 174 | :file-context file-context}) |
175 | 175 |
|
176 | 176 | ;; Validation done. Proceed with form editing |
177 | | - (p/let [balance-result (some-> (parinfer/infer-brackets new-form) |
178 | | - (js->clj :keywordize-keys true)) |
179 | | - form-data (get-ranges-form-data-by-line file-path final-line-number ranges-fn-key) |
180 | | - diagnostics-before-edit (get-diagnostics-for-file file-path)] |
181 | | - (if (:success balance-result) |
182 | | - (p/let [balanced-form (:text balance-result) |
183 | | - balancing-occurred? (not= new-form balanced-form) |
184 | | - text (if (= :insertionPoint ranges-fn-key) |
185 | | - (str (string/trim balanced-form) "\n\n") |
186 | | - balanced-form) |
187 | | - edit-result (edit-replace-range file-path |
188 | | - (first (:ranges-object form-data)) |
189 | | - text) |
190 | | - _ (p/delay 1000) ;; TODO: Consider subscribing on diagnistics changes instead |
191 | | - diagnostics-after-edit (get-diagnostics-for-file file-path)] |
192 | | - (if edit-result |
193 | | - (do |
194 | | - (.save vscode-document) |
195 | | - (cond-> {:success true |
196 | | - :diagnostics-before-edit diagnostics-before-edit |
197 | | - :diagnostics-after-edit diagnostics-after-edit} |
198 | | - (not= final-line-number line-number) |
199 | | - (assoc :actual-line-used final-line-number) |
200 | | - |
201 | | - balancing-occurred? |
202 | | - (merge |
203 | | - {:balancing-note "The code provided for editing had unbalanced brackets. The code was automatically balanced before editing. Use the code in the `balanced-code` to correct your code on record." |
204 | | - :balanced-code balanced-form}))) |
205 | | - {:success false |
206 | | - :diagnostics-before-edit diagnostics-before-edit})) |
207 | | - balance-result)))))) |
| 177 | + (let [validation-result (parinfer/validate-brackets new-form)] |
| 178 | + (if-not (:valid? validation-result) |
| 179 | + ;; REFUSE to edit - return validation failure |
| 180 | + (p/resolved (assoc validation-result :success false)) |
| 181 | + ;; Valid brackets - proceed with edit |
| 182 | + (p/let [form-data (get-ranges-form-data-by-line file-path final-line-number ranges-fn-key) |
| 183 | + diagnostics-before-edit (get-diagnostics-for-file file-path)] |
| 184 | + (p/let [text (if (= :insertionPoint ranges-fn-key) |
| 185 | + (str (string/trim new-form) "\n\n") |
| 186 | + new-form) |
| 187 | + edit-result (edit-replace-range file-path |
| 188 | + (first (:ranges-object form-data)) |
| 189 | + text) |
| 190 | + _ (p/delay 1000) |
| 191 | + diagnostics-after-edit (get-diagnostics-for-file file-path)] |
| 192 | + (if edit-result |
| 193 | + (do |
| 194 | + (.save vscode-document) |
| 195 | + (cond-> {:success true |
| 196 | + :diagnostics-before-edit diagnostics-before-edit |
| 197 | + :diagnostics-after-edit diagnostics-after-edit} |
| 198 | + (not= final-line-number line-number) |
| 199 | + (assoc :actual-line-used final-line-number))) |
| 200 | + {:success false |
| 201 | + :diagnostics-before-edit diagnostics-before-edit}))))))))) |
208 | 202 | (p/catch (fn [e] |
209 | 203 | {:success false |
210 | 204 | :error (.-message e)}))) |
|
265 | 259 | (defn structural-create-file+ |
266 | 260 | "Create a new Clojure file with exact content using vscode/workspace.fs API" |
267 | 261 | [file-path content] |
268 | | - (let [inferred (parinfer/infer-brackets content) |
269 | | - infer-result (when inferred (js->clj inferred :keywordize-keys true)) |
270 | | - balanced-content (if (:success infer-result) |
271 | | - (:text infer-result) |
272 | | - content) |
273 | | - balancing-occurred? (not= content balanced-content)] |
274 | | - (p/let [uri (vscode/Uri.file file-path) |
275 | | - normalized-content (normalize-file-content balanced-content) |
276 | | - content-bytes (.encode (js/TextEncoder.) normalized-content) |
277 | | - directory-path (get-directory-from-path file-path) |
278 | | - directory-uri (vscode/Uri.file directory-path)] |
279 | | - (p/-> (vscode/workspace.fs.createDirectory directory-uri) |
280 | | - (p/catch (fn [_] nil)) ;; Ignore if directory already exists |
281 | | - (p/then (fn [_] |
282 | | - (vscode/workspace.fs.writeFile uri content-bytes))) |
283 | | - (p/then (fn [_] |
284 | | - (p/let [_ (p/delay 1000) ;; Wait for diagnostics to update |
285 | | - diagnostics-after-edit (get-diagnostics-for-file file-path)] |
286 | | - (cond-> {:success true |
287 | | - :file-path file-path |
288 | | - :message "File created successfully" |
289 | | - :diagnostics-after-edit diagnostics-after-edit} |
290 | | - balancing-occurred? |
291 | | - (merge {:balancing-note "The code provided had unbalanced brackets. The code was automatically balanced before creating file." |
292 | | - :balanced-code balanced-content}))))) |
293 | | - (p/catch (fn [error] |
294 | | - {:success false |
295 | | - :error (.-message error) |
296 | | - :file-path file-path})))))) |
| 262 | + (let [validation-result (parinfer/validate-brackets content)] |
| 263 | + (if-not (:valid? validation-result) |
| 264 | + ;; REFUSE to create - return validation failure |
| 265 | + (p/resolved (assoc validation-result |
| 266 | + :file-path file-path)) |
| 267 | + ;; Valid brackets - proceed with file creation |
| 268 | + (p/let [uri (vscode/Uri.file file-path) |
| 269 | + normalized-content (normalize-file-content content) |
| 270 | + content-bytes (.encode (js/TextEncoder.) normalized-content) |
| 271 | + directory-path (get-directory-from-path file-path) |
| 272 | + directory-uri (vscode/Uri.file directory-path)] |
| 273 | + (p/-> (vscode/workspace.fs.createDirectory directory-uri) |
| 274 | + (p/catch (fn [_] nil)) |
| 275 | + (p/then (fn [_] |
| 276 | + (vscode/workspace.fs.writeFile uri content-bytes))) |
| 277 | + (p/then (fn [_] |
| 278 | + (p/let [_ (p/delay 1000) |
| 279 | + diagnostics-after-edit (get-diagnostics-for-file file-path)] |
| 280 | + {:success true |
| 281 | + :file-path file-path |
| 282 | + :message "File created successfully" |
| 283 | + :diagnostics-after-edit diagnostics-after-edit}))) |
| 284 | + (p/catch (fn [error] |
| 285 | + {:success false |
| 286 | + :error (.-message error) |
| 287 | + :file-path file-path}))))))) |
297 | 288 |
|
298 | 289 | (defn append-code+ |
299 | 290 | "Append a top-level form to the end of a file at guaranteed top level" |
300 | 291 | [file-path code] |
301 | | - (let [inferred (parinfer/infer-brackets code) |
302 | | - infer-result (when inferred (js->clj inferred :keywordize-keys true)) |
303 | | - balanced-form (if (:success infer-result) |
304 | | - (:text infer-result) |
305 | | - code) |
306 | | - balancing-occurred? (not= code balanced-form)] |
307 | | - (-> (p/let [uri (vscode/Uri.file file-path) |
308 | | - ^js vscode-document (vscode/workspace.openTextDocument uri) |
309 | | - diagnostics-before-edit (get-diagnostics-for-file file-path) |
310 | | - last-line-number (.-lineCount vscode-document) |
311 | | - end-position (vscode/Position. last-line-number 0) |
312 | | - last-line-text (if (pos? last-line-number) |
313 | | - (.-text (.lineAt vscode-document (dec last-line-number))) |
314 | | - "") |
315 | | - needs-spacing? (and (pos? last-line-number) |
316 | | - (not (string/blank? last-line-text))) |
317 | | - spacing (if needs-spacing? "\n\n" "\n") |
318 | | - append-text (str spacing (string/trim balanced-form) "\n") |
319 | | - edit (vscode/TextEdit.insert end-position append-text) |
320 | | - workspace-edit (vscode/WorkspaceEdit.) |
321 | | - _ (.set workspace-edit uri #js [edit]) |
322 | | - edit-result (vscode/workspace.applyEdit workspace-edit) |
323 | | - _ (p/delay 1000) ;; Wait for diagnostics to update |
324 | | - diagnostics-after-edit (get-diagnostics-for-file file-path)] |
325 | | - (.save vscode-document) |
326 | | - (if edit-result |
327 | | - (cond-> {:success true |
328 | | - :appended-at-end true |
329 | | - :diagnostics-before-edit diagnostics-before-edit |
330 | | - :diagnostics-after-edit diagnostics-after-edit} |
331 | | - balancing-occurred? |
332 | | - (merge {:balancing-note "The code provided had unbalanced brackets. The code was automatically balanced before appending." |
333 | | - :balanced-code balanced-form})) |
334 | | - {:success false |
335 | | - :diagnostics-before-edit diagnostics-before-edit |
336 | | - :error "Failed to apply workspace edit"})) |
337 | | - (p/catch (fn [error] |
338 | | - {:success false |
339 | | - :error (.-message error)}))))) |
| 292 | + (let [validation-result (parinfer/validate-brackets code)] |
| 293 | + (if-not (:valid? validation-result) |
| 294 | + ;; REFUSE to append - return validation failure |
| 295 | + (p/resolved validation-result) |
| 296 | + ;; Valid brackets - proceed with append |
| 297 | + (-> (p/let [uri (vscode/Uri.file file-path) |
| 298 | + ^js vscode-document (vscode/workspace.openTextDocument uri) |
| 299 | + diagnostics-before-edit (get-diagnostics-for-file file-path) |
| 300 | + last-line-number (.-lineCount vscode-document) |
| 301 | + end-position (vscode/Position. last-line-number 0) |
| 302 | + last-line-text (if (pos? last-line-number) |
| 303 | + (.-text (.lineAt vscode-document (dec last-line-number))) |
| 304 | + "") |
| 305 | + needs-spacing? (and (pos? last-line-number) |
| 306 | + (not (string/blank? last-line-text))) |
| 307 | + spacing (if needs-spacing? "\n\n" "\n") |
| 308 | + append-text (str spacing (string/trim code) "\n") |
| 309 | + edit (vscode/TextEdit.insert end-position append-text) |
| 310 | + workspace-edit (vscode/WorkspaceEdit.) |
| 311 | + _ (.set workspace-edit uri #js [edit]) |
| 312 | + edit-result (vscode/workspace.applyEdit workspace-edit) |
| 313 | + _ (p/delay 1000) |
| 314 | + diagnostics-after-edit (get-diagnostics-for-file file-path)] |
| 315 | + (.save vscode-document) |
| 316 | + (if edit-result |
| 317 | + {:success true |
| 318 | + :appended-at-end true |
| 319 | + :diagnostics-before-edit diagnostics-before-edit |
| 320 | + :diagnostics-after-edit diagnostics-after-edit} |
| 321 | + {:success false |
| 322 | + :diagnostics-before-edit diagnostics-before-edit |
| 323 | + :error "Failed to apply workspace edit"})) |
| 324 | + (p/catch (fn [error] |
| 325 | + {:success false |
| 326 | + :error (.-message error)})))))) |
0 commit comments