Skip to content

Commit

Permalink
Remove safe navigation macro (#128)
Browse files Browse the repository at this point in the history
* Remove safe navigation macro
  • Loading branch information
RblSb committed Aug 5, 2024
1 parent 9d56446 commit 90dc446
Show file tree
Hide file tree
Showing 26 changed files with 103 additions and 106 deletions.
36 changes: 18 additions & 18 deletions src/haxeLanguageServer/Context.hx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Context {
}

public function startProgress(title:String):() -> Void {
if (capabilities.window!.workDoneProgress == false) {
if (capabilities.window?.workDoneProgress == false) {
return function() {};
}
final id = progressId++;
Expand Down Expand Up @@ -162,7 +162,7 @@ class Context {
}
capabilities = params.capabilities;
final initOptions:Null<InitOptions> = params.initializationOptions;
experimental = initOptions!.experimentalClientCapabilities ?? {};
experimental = initOptions?.experimentalClientCapabilities ?? {};
config.onInitialize(params);
serverRecording.onInitialize(this);

Expand All @@ -172,8 +172,8 @@ class Context {
new ColorProviderFeature(this);
new InlayHintFeature(this);

final textDocument = capabilities!.textDocument;
final workspace = capabilities!.workspace;
final textDocument = capabilities?.textDocument;
final workspace = capabilities?.workspace;
final registrations = new Array<Registration>();
function register<P, R, PR, E, RO>(method:ProtocolRequestType<P, R, PR, E, RO>, ?registerId:String, ?selector:DocumentSelector, ?registerOptions:RO) {
if (registerOptions == null) {
Expand All @@ -195,7 +195,7 @@ class Context {
};

final completionTriggerCharacters = [".", "@", ":", " ", ">", "$"];
if (textDocument!.completion!.dynamicRegistration == true) {
if (textDocument?.completion?.dynamicRegistration == true) {
register(CompletionRequest.type, "haxeDocument/completion", {
documentSelector: haxeSelector,
triggerCharacters: completionTriggerCharacters,
Expand All @@ -214,7 +214,7 @@ class Context {
}

final signatureHelpTriggerCharacters = ["(", ","];
if (textDocument!.signatureHelp!.dynamicRegistration == true) {
if (textDocument?.signatureHelp?.dynamicRegistration == true) {
register(SignatureHelpRequest.type, {
documentSelector: haxeSelector,
triggerCharacters: signatureHelpTriggerCharacters
Expand All @@ -225,53 +225,53 @@ class Context {
}
}

if (textDocument!.definition!.dynamicRegistration == true) {
if (textDocument?.definition?.dynamicRegistration == true) {
register(DefinitionRequest.type, haxeSelector);
} else {
capabilities.definitionProvider = true;
}

if (textDocument!.hover!.dynamicRegistration == true) {
if (textDocument?.hover?.dynamicRegistration == true) {
register(HoverRequest.type, "haxeDocument/hover", haxeSelector);
register(HoverRequest.type, "hxmlDocument/hover", hxmlSelector);
} else {
capabilities.hoverProvider = true;
}

if (textDocument!.references!.dynamicRegistration == true) {
if (textDocument?.references?.dynamicRegistration == true) {
register(ReferencesRequest.type, haxeSelector);
} else {
capabilities.referencesProvider = true;
}

if (textDocument!.documentSymbol!.dynamicRegistration == true) {
if (textDocument?.documentSymbol?.dynamicRegistration == true) {
register(DocumentSymbolRequest.type, haxeSelector);
} else {
capabilities.documentSymbolProvider = true;
}

if (workspace!.symbol!.dynamicRegistration == true) {
if (workspace?.symbol?.dynamicRegistration == true) {
register(WorkspaceSymbolRequest.type, haxeSelector);
} else {
capabilities.workspaceSymbolProvider = true;
}

if (textDocument!.formatting!.dynamicRegistration == true) {
if (textDocument?.formatting?.dynamicRegistration == true) {
register(DocumentFormattingRequest.type, haxeSelector);
} else {
capabilities.documentFormattingProvider = true;
}

if (textDocument!.rangeFormatting!.dynamicRegistration == true) {
if (textDocument?.rangeFormatting?.dynamicRegistration == true) {
register(DocumentRangeFormattingRequest.type, haxeSelector);
} else {
capabilities.documentRangeFormattingProvider = true;
}

if (textDocument!.rename!.dynamicRegistration == true) {
if (textDocument?.rename?.dynamicRegistration == true) {
register(RenameRequest.type, {documentSelector: haxeSelector, prepareProvider: true});
} else {
if (textDocument!.rename!.prepareSupport == true) {
if (textDocument?.rename?.prepareSupport == true) {
capabilities.renameProvider = {
prepareProvider: true
};
Expand All @@ -280,20 +280,20 @@ class Context {
}
}

if (textDocument!.foldingRange!.dynamicRegistration == true) {
if (textDocument?.foldingRange?.dynamicRegistration == true) {
register(FoldingRangeRequest.type, haxeSelector);
} else {
capabilities.foldingRangeProvider = true;
}

if (textDocument!.colorProvider!.dynamicRegistration == true) {
if (textDocument?.colorProvider?.dynamicRegistration == true) {
// this registration covers both documentColor and colorPresentation
register(DocumentColorRequest.type, haxeSelector);
} else {
capabilities.colorProvider = true;
}

if (textDocument!.inlayHint!.dynamicRegistration == true) {
if (textDocument?.inlayHint?.dynamicRegistration == true) {
register(InlayHintRequest.type, haxeSelector);
} else {
capabilities.inlayHintProvider = true;
Expand Down
6 changes: 3 additions & 3 deletions src/haxeLanguageServer/features/haxe/GotoDefinitionFeature.hx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class GotoDefinitionFeature {
locations = [];
resolve(locations.map(location -> {
final document = getHaxeDocument(location.file.toUri());
final tokens = document!.tokens;
final tokens = document?.tokens;
var previewDeclarationRange = location.range;
if (document != null && tokens != null) {
final targetToken = tokens!.getTokenAtOffset(document.offsetAt(location.range.start));
final pos = targetToken!.parent!.getPos();
final targetToken = tokens?.getTokenAtOffset(document.offsetAt(location.range.start));
final pos = targetToken?.parent?.getPos();
if (pos != null)
previewDeclarationRange = document.rangeAt(pos.min, pos.max);
}
Expand Down
10 changes: 5 additions & 5 deletions src/haxeLanguageServer/features/haxe/InlayHintFeature.hx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class InlayHintFeature {
var endPos = doc.offsetAt(params.range.end);

var inlayHints:Array<InlayHint> = [];
var root:Null<TokenTree> = doc!.tokens!.tree;
var root:Null<TokenTree> = doc?.tokens?.tree;
if (root == null) {
return reject.noFittingDocument(uri);
}
Expand Down Expand Up @@ -292,7 +292,7 @@ class InlayHintFeature {
if (pOpen.access().parent().matches(Kwd(KwdNew)).exists()) {
return promises;
}
var pClose:Null<TokenTree> = pOpen.access().firstOf(PClose)!.token;
var pClose:Null<TokenTree> = pOpen.access().firstOf(PClose)?.token;
if (pClose == null) {
return promises;
}
Expand Down Expand Up @@ -564,19 +564,19 @@ class InlayHintFeature {
}

function buildParameterName<T>(hover:HoverDisplayItemOccurence<T>):Null<String> {
return hover.expected!.name!.name;
return hover.expected?.name?.name;
}

function buildTypeHint<T>(hover:HoverDisplayItemOccurence<T>):Null<String> {
var type = hover.item!.type;
var type = hover.item?.type;
if (type == null) {
return null;
}
return printer.printType(type);
}

function buildReturnTypeHint<T>(hover:HoverDisplayItemOccurence<T>):Null<String> {
var type = hover.item.type!.args!.ret;
var type = hover.item.type?.args?.ret;
if (type == null) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/haxeLanguageServer/features/haxe/RenameFeature.hx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RenameFeature {
final usageContext:refactor.discover.UsageContext = makeUsageContext();
usageContext.fileName = filePath.toString();

var root:Null<TokenTree> = doc!.tokens!.tree;
var root:Null<TokenTree> = doc?.tokens?.tree;
if (root == null) {
usageContext.usageCollector.parseFile(ByteData.ofString(doc.content), usageContext);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/haxeLanguageServer/features/haxe/SignatureHelpFeature.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SignatureHelpFeature {

public function new(context:Context) {
this.context = context;
labelOffsetSupport = context.capabilities.textDocument!.signatureHelp!.signatureInformation!.parameterInformation!.labelOffsetSupport == true;
labelOffsetSupport = context.capabilities.textDocument?.signatureHelp?.signatureInformation?.parameterInformation?.labelOffsetSupport == true;
context.languageServerProtocol.onRequest(SignatureHelpRequest.type, onSignatureHelp);
}

Expand All @@ -43,7 +43,7 @@ class SignatureHelpFeature {
doc:HaxeDocument) {
var wasAutoTriggered = true;
if (context.haxeServer.haxeVersion >= new SemVer(4, 1, 0)) {
final triggerKind = params!.context!.triggerKind;
final triggerKind = params?.context?.triggerKind;
wasAutoTriggered = switch triggerKind {
case null: false; // err on the side of showing too often for LSP clients that don't support triggerKind
case TriggerCharacter: true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CodeActionFeature {
],
resolveProvider: true
});
hasCommandResolveSupport = context.capabilities.textDocument!.codeAction!.resolveSupport!.properties!.contains("command") ?? false;
hasCommandResolveSupport = context.capabilities.textDocument?.codeAction?.resolveSupport?.properties?.contains("command") ?? false;
if (!hasCommandResolveSupport) {
hasCommandResolveSupport = context.experimental?.forceCommandResolveSupport ?? false;
}
Expand Down Expand Up @@ -78,9 +78,9 @@ class CodeActionFeature {

function onCodeActionResolve(action:CodeAction, token:CancellationToken, resolve:CodeAction->Void, reject:ResponseError<NoData>->Void) {
final data:Null<CodeActionResolveData> = action.data;
final type = data!.type;
final params = data!.params;
final diagnostic = data!.diagnostic;
final type = data?.type;
final params = data?.params;
final diagnostic = data?.diagnostic;
if (type == null || params == null) {
resolve(action);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ExtractVarFeature implements CodeActionContributor {
while (parent != null) {
switch parent.tok {
case DblDot: // not anon structure
return parent.parent!.parent!.tok != BrOpen;
return parent.parent?.parent?.tok != BrOpen;
default:
}
token = parent;
Expand Down Expand Up @@ -227,7 +227,7 @@ class ExtractVarFeature implements CodeActionContributor {
switch parent.tok {
case Dot, QuestionDot:
// skip to start of foo.bar.baz
if (parent.parent!.isCIdent() == true) {
if (parent.parent?.isCIdent() == true) {
parent = parent.parent ?? return [];
}
case DblDot, Binop(_), Kwd(_):
Expand All @@ -237,7 +237,7 @@ class ExtractVarFeature implements CodeActionContributor {
case _:
// end of a.b expr is inside of Dot
final first = token.getFirstChild();
final hasDot = first!.tok == Dot || first!.tok == QuestionDot;
final hasDot = first?.tok == Dot || first?.tok == QuestionDot;
final last = hasDot ? first : token;
return [token, getLastNonCommaToken(last)];
}
Expand All @@ -250,7 +250,7 @@ class ExtractVarFeature implements CodeActionContributor {
final tokens:Array<Null<TokenTree>> = [token];
final firstChild = token.getFirstChild();
// don't extract arrow function args
if (firstChild!.tok == Arrow || parent.access().firstOf(Arrow) != null)
if (firstChild?.tok == Arrow || parent.access().firstOf(Arrow) != null)
return [];
if (firstChild == null)
return tokens;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class TokenTreeUtils {
public static function isAnonStructure(brToken:TokenTree):Bool {
if (brToken.tok == BrClose)
brToken = brToken.parent ?? return false;
final first = brToken!.getFirstChild() ?? return false;
final first = brToken?.getFirstChild() ?? return false;
final colon = first.getFirstChild() ?? return false;
if (colon.tok.match(DblDot) && !colon.nextSibling!.tok.match(Semicolon)) {
if (colon.tok.match(DblDot) && !colon.nextSibling?.tok.match(Semicolon)) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AddTypeHintActions {
final uri = params.textDocument.uri;
final doc = context.documents.getHaxe(uri) ?? return [];
final actions:Array<CodeAction> = [];
final token = doc.tokens!.getTokenAtOffset(doc.offsetAt(params.range.end));
final token = doc.tokens?.getTokenAtOffset(doc.offsetAt(params.range.end));
if (token == null)
return [];

Expand Down Expand Up @@ -43,7 +43,7 @@ class AddTypeHintActions {
if (!token.tok.match(Kwd(KwdReturn)))
return actions;

final nameToken = token.parent!.parent ?? return actions;
final nameToken = token.parent?.parent ?? return actions;
// check return type hint
if (nameToken.access().firstOf(DblDot) != null)
return actions;
Expand Down Expand Up @@ -73,7 +73,7 @@ class AddTypeHintActions {
return null;
var tokenSource = new CancellationTokenSource();

final identToken = document.tokens!.getTokenAtOffset(document.offsetAt(params.range.end));
final identToken = document.tokens?.getTokenAtOffset(document.offsetAt(params.range.end));
if (identToken == null)
return null;

Expand Down Expand Up @@ -113,7 +113,7 @@ class AddTypeHintActions {
}
?? cast return null;
final locDoc = context.documents.getHaxe(location.uri) ?? cast return null;
final locToken = locDoc.tokens!.getTokenAtOffset(locDoc.offsetAt(location.range.end));
final locToken = locDoc.tokens?.getTokenAtOffset(locDoc.offsetAt(location.range.end));
if (locToken == null)
return null;
final child = locToken.getFirstChild();
Expand All @@ -135,7 +135,7 @@ class AddTypeHintActions {
final hover:HoverDisplayItemOccurence<Dynamic> = results[1];
final definition = definitions[0] ?? return action;
final defDoc = context.documents.getHaxe(definition.targetUri) ?? return action;
final defToken = defDoc.tokens!.getTokenAtOffset(defDoc.offsetAt(definition.targetSelectionRange.end));
final defToken = defDoc.tokens?.getTokenAtOffset(defDoc.offsetAt(definition.targetSelectionRange.end));
// check if definition already has typehint
if (defToken == null || !defToken.isCIdent())
return action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ChangeFinalToVarAction {
return null;
var tokenSource = new CancellationTokenSource();

final varToken = document.tokens!.getTokenAtOffset(document.offsetAt(diagnostic.range.start));
final varToken = document.tokens?.getTokenAtOffset(document.offsetAt(diagnostic.range.start));
if (varToken == null)
return null;
final gotoPromise = new Promise(function(resolve:(definitions:Array<DefinitionLink>) -> Void, reject) {
Expand All @@ -34,7 +34,7 @@ class ChangeFinalToVarAction {
final definitionDoc = context.documents.getHaxe(definition.targetUri);
if (definitionDoc == null)
return action;
final varDefinitionToken = definitionDoc.tokens!.getTokenAtOffset(definitionDoc.offsetAt(definition.targetSelectionRange.start));
final varDefinitionToken = definitionDoc.tokens?.getTokenAtOffset(definitionDoc.offsetAt(definition.targetSelectionRange.start));
final kwdFinal = getFinalKwd(varDefinitionToken) ?? return action;
final range = document.rangeAt(kwdFinal.pos.min, kwdFinal.pos.max, Utf8);
action.edit = WorkspaceEditHelper.create(definitionDoc, [{range: range, newText: "var"}]);
Expand All @@ -44,7 +44,7 @@ class ChangeFinalToVarAction {
}

static function getFinalKwd(token:Null<TokenTree>) {
final kwdFinal = token!.parent;
final kwdFinal = token?.parent;
if (kwdFinal == null)
return null;
if (!kwdFinal.tok.match(Kwd(KwdFinal)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CompilerErrorActions {
final document = context.documents.getHaxe(params.textDocument.uri);
if (document.tokens != null) {
// Resolve parent token to add "override" before "function" instead of function name
final funPos = document.tokens!.getTokenAtOffset(document.offsetAt(diagnostic.range.start))!.parent!.pos!.min;
final funPos = document.tokens?.getTokenAtOffset(document.offsetAt(diagnostic.range.start))?.parent?.pos?.min;
if (funPos != null) {
pos = document.positionAt(funPos, Utf8);
}
Expand Down
Loading

0 comments on commit 90dc446

Please sign in to comment.