Skip to content

Commit

Permalink
Fixes #339
Browse files Browse the repository at this point in the history
  • Loading branch information
isc-bsaviano committed Aug 12, 2024
1 parent ea5780b commit 42eb61f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Fix issue [#336](https://github.com/intersystems/language-server/issues/336): Add intellisense for variables set to the returned value of a method, or a property, with a declared type
- Fix issue [#337](https://github.com/intersystems/language-server/issues/337): Improve Hover headers
- Fix issue [#338](https://github.com/intersystems/language-server/issues/338): Add intellisense for macros in Embedded SQL
- Fix issue [#339](https://github.com/intersystems/language-server/issues/339): Macro intellisense doesn't respect `#undef` directive

## [2.5.1] - 2024-07-09
- Fix issue [#328](https://github.com/intersystems/language-server/issues/328): Fix namespace detection for Diagnostic computation
Expand Down
30 changes: 19 additions & 11 deletions server/src/providers/completion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompletionItem, CompletionItemKind, CompletionItemTag, CompletionParams, InsertTextFormat, Position, Range, TextEdit } from 'vscode-languageserver/node';
import { CompletionItem, CompletionItemKind, CompletionItemTag, CompletionParams, InsertTextFormat, MarkupKind, Position, Range, TextEdit } from 'vscode-languageserver/node';
import { getServerSpec, getLanguageServerSettings, getMacroContext, makeRESTRequest, normalizeSystemName, getImports, findFullRange, getClassMemberContext, quoteUDLIdentifier, documaticHtmlToMarkdown, determineClassNameParameterClass, storageKeywordsKeyForToken, getParsedDocument, currentClass, normalizeClassname } from '../utils/functions';
import { ServerSpec, QueryData, KeywordDoc, MacroContext, compressedline } from '../utils/types';
import { documents, corePropertyParams } from '../utils/variables';
Expand Down Expand Up @@ -759,20 +759,24 @@ export async function onCompletion(params: CompletionParams): Promise<Completion
}

// Scan up through the file, looking for macro definitions
const undefs: string[] = [];
for (let ln = params.position.line-1; ln >= 0; ln--) {
if (parsed[ln].length < 4) {
continue;
}
if (parsed[ln][0].l == ld.cos_langindex && parsed[ln][0].s == ld.cos_ppc_attrindex) {
if (!parsed[ln]?.length) continue;
if (parsed[ln].length > 1 && parsed[ln][0].l == ld.cos_langindex && parsed[ln][0].s == ld.cos_ppc_attrindex) {
// This line begins with a preprocessor command
const ppctext = doc.getText(Range.create(
ln,parsed[ln][1].p,
ln,parsed[ln][1].p+parsed[ln][1].c
)).toLowerCase();
if (ppctext == "define" || ppctext == "def1arg") {
if (parsed[ln].length > 3 && ["define","def1arg"].includes(ppctext)) {
// This is a macro definition
const macro = doc.getText(Range.create(ln,parsed[ln][2].p,ln,parsed[ln][2].p+parsed[ln][2].c));
if (undefs.includes(macro)) {
// Don't suggest this macro because it was #undef'd before the completion line
continue;
}
const macrodef: CompletionItem = {
label: doc.getText(Range.create(ln,parsed[ln][2].p,ln,parsed[ln][2].p+parsed[ln][2].c)),
label: macro,
kind: CompletionItemKind.Text,
data: ["macro",doc.uri]
};
Expand Down Expand Up @@ -830,7 +834,7 @@ export async function onCompletion(params: CompletionParams): Promise<Completion
}
if (docstr != macrodef.label) {
macrodef.documentation = {
kind: "plaintext",
kind: MarkupKind.PlainText,
value: docstr
};
}
Expand All @@ -852,14 +856,18 @@ export async function onCompletion(params: CompletionParams): Promise<Completion
const valmatchres = restofline.match(valregex);
if (valmatchres !== null) {
macrodef.documentation = {
kind: "plaintext",
kind: MarkupKind.PlainText,
value: docstr + "\n" + valmatchres[1]
};
}
}
result.push(macrodef);
} else if (parsed[ln].length > 2 && ppctext == "undef") {
// This is a macro un-definition
undefs.push(doc.getText(Range.create(ln,parsed[ln][2].p,ln,parsed[ln][2].p+parsed[ln][2].c)));
}
}
if (parsed[ln].some((t) => t.l == ld.cls_langindex)) break;
}
}
else if (prevline.slice(-1) === "$" && prevline.charAt(prevline.length-2) !== "$" && triggerlang === ld.cos_langindex) {
Expand Down Expand Up @@ -2127,7 +2135,7 @@ export async function onCompletionResolve(item: CompletionItem): Promise<Complet
if (respdata !== undefined && respdata.data.result.content.length > 0) {
// The class was found
item.documentation = {
kind: "markdown",
kind: MarkupKind.Markdown,
value: documaticHtmlToMarkdown(respdata.data.result.content[0].Description)
};
}
Expand Down Expand Up @@ -2156,7 +2164,7 @@ export async function onCompletionResolve(item: CompletionItem): Promise<Complet
defstr = defstr.concat(parts[0],"\n",parts.slice(1).join());
}
item.documentation = {
kind: "plaintext",
kind: MarkupKind.PlainText,
value: defstr
};
}
Expand Down
33 changes: 15 additions & 18 deletions server/src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,32 +1116,29 @@ export async function createDefinitionUri(paramsUri: string, filename: string, e
* @param macro The selected macro.
*/
export function isMacroDefinedAbove(doc: TextDocument, parsed: compressedline[], line: number, macro: string): number {
var result: number = -1;
let result: number = -1;

// Scan up through the file, looking for macro definitions
for (let ln = line-1; ln >= 0; ln--) {
if (parsed[ln].length < 4) {
continue;
}
if (parsed[ln][0].l == ld.cos_langindex && parsed[ln][0].s == ld.cos_ppc_attrindex) {
if (!parsed[ln]?.length) continue;
if (parsed[ln].length > 1 && parsed[ln][0].l == ld.cos_langindex && parsed[ln][0].s == ld.cos_ppc_attrindex) {
// This line begins with a preprocessor command
const ppctext = doc.getText(Range.create(
Position.create(ln,parsed[ln][1].p),
Position.create(ln,parsed[ln][1].p+parsed[ln][1].c)
ln,parsed[ln][1].p,
ln,parsed[ln][1].p+parsed[ln][1].c
)).toLowerCase();
if (ppctext === "define" || ppctext === "def1arg") {
// This is a macro definition
const macrotext = doc.getText(Range.create(
Position.create(ln,parsed[ln][2].p),
Position.create(ln,parsed[ln][2].p+parsed[ln][2].c)
));
if (macrotext === macro) {
// We found the definition for the selected macro
result = ln;
break;
}
if (
parsed[ln].length > 2 && ["define","def1arg","undef"].includes(ppctext) && doc.getText(Range.create(
ln,parsed[ln][2].p,
ln,parsed[ln][2].p+parsed[ln][2].c
)) == macro
) {
// We found the (un-)definition for the selected macro
if (ppctext != "undef") result = ln;
break;
}
}
if (parsed[ln].some((t) => t.l == ld.cls_langindex)) break;
}

return result;
Expand Down

0 comments on commit 42eb61f

Please sign in to comment.