Adjust autocomplete to new file layout

This commit is contained in:
ochafik 2023-03-27 00:23:34 +01:00
commit 6e05266da2

View file

@ -1,7 +1,7 @@
// Portions of this file are Copyright 2021 Google LLC, and licensed under GPL2+. See COPYING. // Portions of this file are Copyright 2021 Google LLC, and licensed under GPL2+. See COPYING.
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import { readDirAsArray, Symlinks } from '../fs/filesystem'; import { join, readDirAsArray, Symlinks } from '../fs/filesystem';
import { ParsedFile, ParsedFunctionoidDef, parseOpenSCAD, stripComments } from './openscad-pseudoparser'; import { ParsedFile, ParsedFunctionoidDef, parseOpenSCAD, stripComments } from './openscad-pseudoparser';
import builtinSignatures from './openscad-builtins' import builtinSignatures from './openscad-builtins'
import { mapObject } from '../utils'; import { mapObject } from '../utils';
@ -124,16 +124,21 @@ export async function buildOpenSCADCompletionItemProvider(fs: FS, workingDir: st
const dir = (path.split('/').slice(0, -1).join('/') || '.') + '/'; const dir = (path.split('/').slice(0, -1).join('/') || '.') + '/';
const handleInclude = async (isUse: boolean, otherPath: string) => { const handleInclude = async (isUse: boolean, otherPath: string) => {
for (const path of [`${dir}/${otherPath}`, otherPath]) { let found = false;
for (const option of [`/libraries/${otherPath}`, `${dir}/${otherPath}`, otherPath]) {
try { try {
const otherSrc = await readFile(otherPath); const otherSrc = await readFile(option);
const sub = await getParsed(otherPath, otherSrc, {skipPrivates: true, addBuiltins: false}); const sub = await getParsed(otherPath, otherSrc, {skipPrivates: true, addBuiltins: false});
mergeDefinitions(isUse, sub); mergeDefinitions(isUse, sub);
found = true;
break;
} catch (e) { } catch (e) {
// console.warn(path, e); console.warn(`Failed to read file option ${option} for ${otherPath} ${isUse ? 'used' : 'included'} by ${path}`, e);
} }
} }
console.error('Failed to find ', otherPath, '(context imported in ', path, ')'); if (!found) {
console.error('Failed to find ', otherPath, '(context imported in ', path, ')');
}
}; };
if (addBuiltins && path != builtinsPath) { if (addBuiltins && path != builtinsPath) {
@ -180,54 +185,62 @@ export async function buildOpenSCADCompletionItemProvider(fs: FS, workingDir: st
folderPrefix = prefix.substring(0, i); folderPrefix = prefix.substring(0, i);
filePrefix = prefix.substring(i + 1); filePrefix = prefix.substring(i + 1);
} }
folder = workingDir + (folderPrefix == '' ? '' : '/' + folderPrefix); const folderName = (folderPrefix == '' ? '' : '/' + folderPrefix);
let files = folderPrefix == '' ? [...Object.keys(allSymlinks)] : []; let files: string[] | null = null
try { for (const folder of [join('/libraries', folderName), join(workingDir, folderName)]) {
files = [...(await readDirAsArray(fs, folder) ?? []), ...files]; files = folderPrefix == '' ? [...Object.keys(allSymlinks)] : [];
// console.log('readDir', folder, files); try {
} catch (e) { files = [...(await readDirAsArray(fs, folder) ?? []), ...files];
console.error(e); // console.log('readDir', folder, files);
break;
} catch (e) {
//console.error(e);
}
} }
const suggestions = []; const suggestions = [];
for (const file of files) { if (!files) {
if (filePrefix != '' && !file.startsWith(filePrefix)) { console.warn('Failed to find folder named ' + folderName);
continue; } else {
} for (const file of files) {
if (/^(LICENSE.*|fonts)$/.test(file)) { if (filePrefix != '' && !file.startsWith(filePrefix)) {
continue; continue;
} }
if (folderPrefix == '' && (file in zipArchives) && zipArchives[file].symlinks) { if (/^(LICENSE.*|fonts)$/.test(file)) {
continue; continue;
} }
const isFolder = !file.endsWith('.scad'); if (folderPrefix == '' && (file in zipArchives) && zipArchives[file].symlinks) {
const completion = file + (isFolder ? '' : '>\n'); // don't append '/' as it's a useful trigger char continue;
}
const isFolder = !file.endsWith('.scad');
const completion = file + (isFolder ? '' : '>\n'); // don't append '/' as it's a useful trigger char
console.log(JSON.stringify({ console.log(JSON.stringify({
prefix, prefix,
folder, folder,
filePrefix, filePrefix,
folderPrefix, folderPrefix,
// files, // files,
completion, completion,
file, file,
}, null, 2)); }, null, 2));
suggestions.push({ suggestions.push({
label: file, label: file,
kind: isFolder ? monaco.languages.CompletionItemKind.Folder : monaco.languages.CompletionItemKind.File, kind: isFolder ? monaco.languages.CompletionItemKind.Folder : monaco.languages.CompletionItemKind.File,
insertText: completion insertText: completion
}); });
}
} }
suggestions.sort(); suggestions.sort();
return { suggestions }; return { suggestions };
} }
const inputFile = workingDir + "/foo.scad"; const inputFile = join(workingDir, 'foo.scad');
delete parsedFiles[inputFile]; delete parsedFiles[inputFile];
const parsed = await getParsed(inputFile, text, {skipPrivates: false, addBuiltins: true}); // const parsed = await getParsed(inputFile, text, {skipPrivates: false, addBuiltins: true});
// console.log("PARSED", JSON.stringify(parsed, null, 2)); const parsed = await getParsed(inputFile, text, {skipPrivates: false, addBuiltins: false});
console.log("PARSED", JSON.stringify(parsed, null, 2));
type CompletionItem = monaco.languages.CompletionItem & { type CompletionItem = monaco.languages.CompletionItem & {
range?: monaco.IRange range?: monaco.IRange